我有一个用于我正在开发的应用程序的 UITableView
,它连接到导航 Controller 和允许用户将动物添加到 tableview 的其他 View 。这是通过另一个 tableViewController 完成的,该 Controller 在用户点击完成(展开转场)时将数据加载到动物表中。一切都应该非常简单,我设置的示例数据如下所示:
但是在添加数据过程之后(在运行应用程序时完成),测试数据如下所示:
我已经通过为单元格高度编写像这样的小函数来解决一些布局问题:
override func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100 //Whatever fits your need for that cell
}
关于如何让表格 View 以相同的布局显示所有信息的任何想法,无论实现的数据来自何处(示例或在应用程序中添加)?
我的 cellForRowAtIndexPath
('CatCell' 是表格单元格,'Cat' 是名称和品种结构):
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CatCell", forIndexPath: indexPath)
as! CatCell
let cat = cats[indexPath.row] as Cat
cell.cat = cat
return cell
}
UITableViewController
由以下部分组成:
用户点击完成后,tableView 会像这样更新:
@IBAction func doneToMyCats(segue:UIStoryboardSegue) {
if let catDetailsViewController = segue.sourceViewController as? CatDetailsViewController {
//add the new cat to the cats array
if let cat = catDetailsViewController.cat {
cats.append(cat)
//update the tableView
let indexPath = NSIndexPath(forRow: cats.count-1, inSection: 0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
}
}
最佳答案
所以尝试使用 tableView 重新加载数据方法,而不是像你写的那样插入你的单元格:
@IBAction func doneToMyCats(segue:UIStoryboardSegue) {
if let catDetailsViewController = segue.sourceViewController as? CatDetailsViewController {
//add the new cat to the cats array
if let cat = catDetailsViewController.cat {
cats.append(cat)
//update the tableView
tableView.reloadData()
}
}
关于ios - UITableView 添加的数据不遵循与示例数据相同的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33753220/