我应该如何实现自定义表格视图单元格? [英] How should I implement a custom table view cell?

查看:31
本文介绍了我应该如何实现自定义表格视图单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 tableview 中的单元格从通用更改为自定义,并创建了一个新的 tableview 单元格类.但是,我无法让单元格识别新的自定义单元格.相反,它仍然显示通用单元格,尽管我已经在识别检查器中将故事板中的设置更改为自定义,并将 tableview 单元格的类更改为新的自定义单元格.我还将故事板中单元格中的元素连接到新的自定义类.

I am trying to change a cell in a tableview from generic to custom and have created a new tableview cell class. However, I cannot get the cell to recognize the new custom cell. Instead, it is still displaying the generic cell although I've changed the setting in storyboard to custom in the identify inspector and also changed the class for the tableview cell to the new custom one. I have also wired the elements in the cell in storyboard to the new custom class.

我的理解是 tableview VC 知道从 tableView cellForRowAtIndexPath 方法使用哪个类,我也用下面的代码改变了它.项目编译但继续显示旧的通用单元格.任何人都可以看到错误或建议其他操作吗?

My understanding is the tableview VC knows which class to use from the tableView cellForRowAtIndexPath method and I have changed that too with the code below. The project compiles but continues to show old generic cell. Can anyone see error or suggest what else to do?

#import "customCell.h"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  customCell *cell = (customCell *)[self.tableView dequeueReusableCellWithIdentifier:@"Cell"];
    Items *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
    
    return cell;
}

预先感谢您的任何建议.

Thanks in advance for any suggestions.

通过拖动uielements在tableview顶部的storyboard中创建一个自定义单元格.也许这是一个问题:

A custom cell is created in the storyboard on top of tableview by dragging uielements. Maybe this is an issue:

单元格样式设置为自定义:

Style of cell is set to Custom:

推荐答案

您需要创建新的 UITableViewCell 并启用创建 XIB 文件...".在 XIB 中创建您的自定义单元格,并使其成为您刚刚创建的类的自定义类.

You need to create new UITableViewCell and enable the "create XIB file...". Create your custom cell in the XIB and make it's custom class as your class you just created.

然后在 tableView cellForRowAtIndexPath 中注册 Nib:(它很快,但我打赌你可以找出目标 c 并行......)

Then in tableView cellForRowAtIndexPath register the Nib: (it's in swift but I bet you can figure out the objective c parallel...)

tableView.registerNib(UINib(nibName: "YourUITableViewCellClass", bundle: nil), forCellReuseIdentifier: "YourCustomIdentifierFromTheCustomClass")

然后访问您的单元格:

var cell = tableView.dequeueReusableCellWithIdentifier("YourCustomIdentifierFromTheCustomClass")

就是这样.

这篇关于我应该如何实现自定义表格视图单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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