2015-09-06 253 views
1

我正在创建一个类似于Yelp的应用程序。在一个视图控制器中,我有2个视图:带有名为'list'和mapView的插座的UITableView。在顶部我有一个搜索栏。使用数据填充TableView

填充表视图通常很简单,但我没有使用UITableViewController,所以它有点混乱。

在最高层,我添加了一个全局变量

var cell: UITableViewCell? 

然后在表格的功能,我有

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 



    var cell = tableView.dequeueReusableCellWithIdentifier("attractionCell") as! AttractionCell! 

    if cell == nil { 
    cell = AttractionCell(style: UITableViewCellStyle.Default, reuseIdentifier: "attractionCell") 
    } 
    cell.attractionName.text = title 
    return cell 

} 

在底部,我有当搜索栏是一个函数单击,然后它从Parse中获取数据,并将对象的名称分配给名为'title'的变量。

func searchBarSearchButtonClicked(searchBar: UISearchBar) { 
    var Query = PFQuery(className:"Stores") 
    Query.whereKey("Location", nearGeoPoint:area, withinMiles: 5) 
    Query.findObjectsInBackgroundWithBlock { 
    (objects: [AnyObject]?, error: NSError?) -> Void in 

    if error == nil { 

    let objects = objects as! [PFObject] 
    for object in objects {      
    let title = object["name"] as! String 
self.cell?.attractionName.text = title 

为什么我的表格视图不能填充?

此外,在故事板,TableView中的原型细胞是类AttractionCell,并有一个名为attractionCell

+0

它与tableview行数有关吗? – DesignMeetsCode

+0

您是否将tableView链接到委托和数据源?你的控制器符合('UITableViewDelegate'和)'UITableViewDataSource'吗? –

回答

0

所有这行的第一个不应该工作将其删除:

if cell == nil 
    { 
cell = AttractionCell(style: UITableViewCellStyle.Default, reuseIdentifier: "attractionCell") 
    } 

,也拆除全局变量细胞

然后你应该声明一个全局字符串数组

在你的函数210
var arrayData = [String]() 

删除该行:

self.cell?.attractionName.text = title 

并添加此

arrayData.append(title) 
self.tableView.reloadData() 

numberofRowInSection()

return arrayData.count 

的cellForRowAtIndexPath()

cell.attractionName.text = arrayData[indexPath.row] 
+0

对不起,我忘了在搜索功能中提到,我将单元格文本分配给标题。因此,如果我没有单元格作为全局变量,搜索功能将无法识别它 – DesignMeetsCode

+0

我们是否赞成发布您的所有代码和您正在使用的错误 – Lamar

+0

在顶部添加了所有内容 – DesignMeetsCode

0

reuseIdentifier尽量不要写“?”第一个变量...

就像是:

var cell: UITableViewCell 
+0

当我这样做,它说,ViewController没有初始化器 – DesignMeetsCode

+0

当你调用该变量,你必须调用它!为了使它工作 – 2015-09-07 12:20:59