用数据填充 TableView
Populate TableView with Data
我正在开发一个类似于 Yelp 的应用。在视图控制器中,我有 2 个视图:带有一个名为 'list' 的插座的 UITableView 和一个 mapView。在顶部我有一个搜索栏。
填充 table 视图通常很简单,但我没有使用 UITableViewController,所以有点混乱。
在最上面,我添加了一个全局变量
var cell: UITableViewCell?
然后在 table 函数中,我有
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
为什么我的 table 视图没有填充?
此外,在情节提要中,tableView 中的原型单元属于 class AttractionCell,并且有一个名为 attractionCell
的 reuseIdentifier
尽量不要写“?”关于第一个变量...
像那样:
var cell: UITableViewCell
首先,这一行不应该删除它:
if cell == nil
{
cell = AttractionCell(style: UITableViewCellStyle.Default, reuseIdentifier: "attractionCell")
}
并且还删除了全局变量单元格
那么你应该全局声明一个字符串数组
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]
我正在开发一个类似于 Yelp 的应用。在视图控制器中,我有 2 个视图:带有一个名为 'list' 的插座的 UITableView 和一个 mapView。在顶部我有一个搜索栏。
填充 table 视图通常很简单,但我没有使用 UITableViewController,所以有点混乱。
在最上面,我添加了一个全局变量
var cell: UITableViewCell?
然后在 table 函数中,我有
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
为什么我的 table 视图没有填充?
此外,在情节提要中,tableView 中的原型单元属于 class AttractionCell,并且有一个名为 attractionCell
的 reuseIdentifier尽量不要写“?”关于第一个变量...
像那样:
var cell: UITableViewCell
首先,这一行不应该删除它:
if cell == nil
{
cell = AttractionCell(style: UITableViewCellStyle.Default, reuseIdentifier: "attractionCell")
}
并且还删除了全局变量单元格
那么你应该全局声明一个字符串数组
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]