我正在创建一个类似于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
它与tableview行数有关吗? – DesignMeetsCode
您是否将tableView链接到委托和数据源?你的控制器符合('UITableViewDelegate'和)'UITableViewDataSource'吗? –