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 - 移除 UIPickerView 边框

iOS Xcode 10 : Private framework with pods dependencies

ios - dateWithTimeIntervalSince1970 方法返回不同的时区

ios - Swift:无法将类型 'UITableViewCell' 的值转换为自定义单元格类

ios - 当应用程序在 iOS 中处于后台时,openURL 不起作用

ios - 在表格 View 单元格上无法访问detailTextLabel和imageView

ios - 使用 3D touch peek and pop,您如何检测用户在 peek 期间的平移(如 Facebook)?

ios - 找不到委托(delegate)的协议(protocol)声明

ios - TableView 单元格在滚动时消失

ios - 如何将UIDatePicker添加到TableViewCell?