无法让 UITableViewController 动态填充 [英] Can't get UITableViewController to dynamically populate

查看:18
本文介绍了无法让 UITableViewController 动态填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习一些关于如何填充表格视图的基本教程.我在 UITableView 和动态填充方面取得了成功,但在使用 UITableViewController 时,按照相同的步骤对我不起作用.

I'm following some basic tutorials on how to populate a table view. I have had success with a UITableView and dynamic populating, but following the same steps is not working for me with a UITableViewController.

能否解释为什么我在使用控制器时遇到问题?

Any explanation as to why I am having difficulties with the controller?

在 UITableViewController 上似乎没有像 UITableView 那样多的信息.为什么每个人似乎都只倾向于 UITableView?

There doesn't seem to be as much information out there on UITableViewController as there is just UITableView. Is there a reason why everyone seems to be gravitating towards just the UITableView?

为了记录,这是我正在关注的教程:http://www.ioscreator.com/tutorials/tableview-tutorial-in-ios8-with-swift

For the record, here is the tutorial I am following: http://www.ioscreator.com/tutorials/tableview-tutorial-in-ios8-with-swift

import UIKit

class TestTableViewController: UITableViewController, UITableViewDelegate {

    let tableData = ["One", "Two", "Three"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 0
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return countElements(tableData)
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell

        // Configure the cell...
        cell.textLabel?.text = tableData[indexPath.row]

        return cell
    }


    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

}

将 numberOfSectionsInTableView 更改为返回 1 后,这是我收到的错误.

After changing numberOfSectionsInTableView to return 1, this is the error I am receiving.

2015-01-11 18:30:11.557 TableViewTest[30788:8219862] * 断言-[UITableView 中的失败dequeueReusableCellWithIdentifier:forIndexPath:],/SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:6116 2015-01-1118:30:11.560 TableViewTest[30788:8219862] * 由于以下原因终止应用程序未捕获的异常NSInternalInconsistencyException",原因:无法使用标识符重用标识符使单元出列 - 必须注册一个nib 或标识符的类或连接原型单元格中的故事板'

2015-01-11 18:30:11.557 TableViewTest[30788:8219862] * Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:6116 2015-01-11 18:30:11.560 TableViewTest[30788:8219862] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier reuseIdentifier - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

推荐答案

根据您的错误消息,这是解决方案:

Based on your error message, this is the solution:

在 ViewDidLoad() 中,添加:

In ViewDidLoad(), add:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")

在此处查看我的示例项目:https://github.com/ericcgu/EGSwiftTableViewController

See my example project here: https://github.com/ericcgu/EGSwiftTableViewController

这篇关于无法让 UITableViewController 动态填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆