Xcode 7.3 UITableViewController 单元格显示不正确
Xcode 7.3 UITableViewController cell not displaying correctly
我尝试显示一个带有一个标签和一个添加项目按钮的简单单元格。我无法让它正确显示。我花了很多时间,但找不到解决方案。
这是结果:
没有 add item
按钮,也没有正确的行。我在 coredata 中只有两个项目。
这是我的代码:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let data = items[indexPath.row] as Item
cell.textLabel?.text = data.body
return cell
}
有什么问题?谁能帮我显示 add item
按钮、更正行数并正确自定义单元格的高度?提前致谢。
首先让 return 正确的行数进入 table 视图。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
接下来让它输出到原型单元。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = cellContent[indexPath.row]
return cell
}
还可以调用以下视图控制器,以便它可以 运行 正确。
ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
最后一件事,您原型的单元格与代码中的单元格具有相同的标识符。在我的代码中,我会确保原型单元格中的标识符是 "Cell",因为我在 UITableViewCell(...reuseIdentifier: "Cell") 中调用了它。
View picture to see where to add the identifier in your storyboard
我尝试显示一个带有一个标签和一个添加项目按钮的简单单元格。我无法让它正确显示。我花了很多时间,但找不到解决方案。
这是结果:
没有 add item
按钮,也没有正确的行。我在 coredata 中只有两个项目。
这是我的代码:
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let data = items[indexPath.row] as Item
cell.textLabel?.text = data.body
return cell
}
有什么问题?谁能帮我显示 add item
按钮、更正行数并正确自定义单元格的高度?提前致谢。
首先让 return 正确的行数进入 table 视图。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
接下来让它输出到原型单元。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = cellContent[indexPath.row]
return cell
}
还可以调用以下视图控制器,以便它可以 运行 正确。
ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
最后一件事,您原型的单元格与代码中的单元格具有相同的标识符。在我的代码中,我会确保原型单元格中的标识符是 "Cell",因为我在 UITableViewCell(...reuseIdentifier: "Cell") 中调用了它。 View picture to see where to add the identifier in your storyboard