ios - UITableView 添加的数据不遵循与示例数据相同的布局

标签 ios xcode swift uitableview segue

我有一个用于我正在开发的应用程序的 UITableView,它连接到导航 Controller 和允许用户将动物添加到 tableview 的其他 View 。这是通过另一个 tableViewController 完成的,该 Controller 在用户点击完成(展开转场)时将数据加载到动物表中。一切都应该非常简单,我设置的示例数据如下所示:

sample data tableview

但是在添加数据过程之后(在运行应用程序时完成),测试数据如下所示:

new data tableview

我已经通过为单元格高度编写像这样的小函数来解决一些布局问题:

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 由以下部分组成:

storyboard layout

用户点击完成后,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/

相关文章:

swift - 想要在新行中打印数据

ios - 重新设计的 iTunes Connect 中的崩溃报告

ios - SMS 打开消息

ios - 找不到“FMDB.h”文件

xcode - 如何使用LLDB打印内存地址的内容?

java - 有没有获取iOS设备日志的命令?像使用logcat一样我们可以获取Android设备日志

swift - collectionView.reloadData 无法正常运行

iOS - 如何处理应用程序屏幕中存在长文本区域的情况?

ios - 带有 IOS 的 Google IMA HTML5 SDK

ios - Instagram API 从 WebView NSURL 请求获得响应