ios - 如何将自定义类添加到我的 UITableViewController?

标签 ios objective-c uitableview

在我当前的应用程序中,我有 24 个不同的 View ,每个 View 都有自己的 ViewController。随着时间的流逝,我开始意识到将它们更改为 TableViewController 不仅功能更好,而且实际上看起来也更好。

通常,我将自定义类分配给我的 View 的方式是在 Xcode 中转到:File > New > File > Objective-C Class。

我创建类并确保它是 UIViewController 的子类。创建文件后,我在 Storyboard 文件中单击我的 View Controller,转到检查器并将自定义类设置为 myNewViewController,全部完成!

但在使用 UITableView 时情况并非如此,我能够在 Storyboard 文件中添加表格 View ,自定义它/添加部分/单元格等,但是当我想喜欢分配一个我按照上述步骤创建的新类,除了这次我从 UITableViewController

继承

我收到以下警告: enter image description here

这是不完整的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

此外,在我的 iOS 设备上运行时, View 显示为空白。

我知道这个实现必须先完成才能正常运行,但是有没有一种特定的方法可以链接我正在使用的 View 和它的 ViewController?这样就不需要这些配置了?

我下一步要做什么?

感谢您的建议!

编辑 我还尝试将单元格标识符设置为“单元格”,还尝试更改值以便 numberOfSections 返回 5,numberOfRowsInSection 返回 2 但仍然没有成功,应用程序崩溃了,我在调试日志中获取:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
***

最佳答案

您正在使用静态单元格。您不应该使用数据源方法。

关于ios - 如何将自定义类添加到我的 UITableViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17686665/

相关文章:

ios - 如何在 didFinishSavingWithError 之后关闭图像选择器?

ios - 我们可以从 LocalAuthentication ios 生成字符串吗?

iphone - 如何使用第一个字符作为部分名称

iphone - iOS5无法自定义标签栏

ios - 为 iOS 应用程序存储外部数据库的最佳选择

c++ - 如何处理头文件中的非ARC代码?

iphone - 另一个 ScrollView 内的 ScrollView 内的 TableView

objective-c - 明确回答 playerToInvite 的用途

iPhone + UITableView + 访问特定行的单元格

iphone - 如何从表格 View 中完全隐藏某个单元格?