难以访问在 Interface Builder .xib 文件中创建的 UITableViewCell [英] Difficulty Accessing UITableViewCell Created in Interface Builder .xib File

查看:25
本文介绍了难以访问在 Interface Builder .xib 文件中创建的 UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 UITableView 中,对于表格最后一部分的最后一行,我加载了一个特殊的 UITableViewCell,它与表格中的所有其他元素都不同.我在我的 .xib 文件中创建了单元格,并给它重用标识符endCell".我认为我可以执行以下操作来访问我的手机:

In my UITableView, for the last row of the last section of the table, I load a special UITableViewCell that is different from all the others on the table. I created the cell in my .xib file and gave it the reuse identifier "endCell." I would think that I could just do the following to access my cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if ((indexPath.section == [sections count] - 1) && indexPath.row == [[sections objectAtIndex:indexPath.section] count])) {

    return [tableView dequeueReusableCellWithIdentifier:@"endCell"];

} //more code for other cells...

我已经在界面构建器中设置了单元标识符.但是,运行此代码会导致崩溃并出现错误:

I have set the cell identifier in the interface builder. However, running this code causes a crash with an error:

"Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:],..."

在研究了这个错误之后,我能找到的唯一解决方案是通过一个包含 .xib 中包含的对象的数组访问它来获取指向 .xib 单元的指针.所以我尝试了以下方法:

After researching this error, the only solution I could find was to get a pointer to the .xib cell by accessing it through an array that holds the objects contained inside the .xib. So I tried the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if ((indexPath.section == [sections count] - 1) && (indexPath.row == [[sections objectAtIndex:indexPath.section] count]) ) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"endCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]init];
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PLNewConversationViewController" owner:self options:nil];
            for (id xibItem in topLevelObjects) {
                if ([xibItem isKindOfClass:[UITableViewCell class]]){
                    UITableViewCell *tableViewCell = (UITableViewCell *)xibItem;
                    if ([tableViewCell.reuseIdentifier isEqualToString:@"endCell"]) {
                        cell = xibItem;   
                    }
                }
            }
        }

    return  cell;
} //more code for other cells...

这会导致非常奇怪的行为 - 我什至不知道它在做什么 - 最后一个单元格从未显示过,而且它似乎无休止地滚动,因为当它到达表格底部时,它会自动跳回直到桌子的顶部.有时会出现各种错误的崩溃.

This results in very strange behavior - it's not even obvious to me what it is doing - the last cell is never shown, and it appears to be scrolling endlessly because when it gets to the bottom of the table, it automatically jumps back up to the top of the table. Sometimes there is a crash with varying errors.

我设法通过将我的单元格连接到必要的类作为出口并将单元格返回为return self.endCell ..."来解决这个问题,但我觉得我应该能够在没有的情况下访问单元格这样做.

I have managed to work around this by hooking up my cell to the necessary class as an outlet and just returning the cell as "return self.endCell..." but I feel like I should be able to access the cell without doing this.

谁能看到我做错了什么?

Can anyone see what I am doing wrong?

推荐答案

从 nib 文件加载自定义表格视图单元格的最简单方法是在单独 xib 文件(仅包含该单元格)并使用

The easiest way to load custom table view cells from a nib file is to create them in a separate xib file (containing only that cell) and registering the generated nib file with

[self.tableView registerNib:[UINib nibWithNibName:@"YourNibFile" bundle:nil] forCellReuseIdentifier:@"YourReuseIdentifier"];

例如在表视图控制器的 viewDidLoad 中.

for example in viewDidLoad of the table view controller.

[tableView dequeueReusableCellWithIdentifier:@"YourReuseIdentifier"]

如有必要,将从该 nib 文件中实例化单元格.

will then instantiate cells from that nib file if necessary.

这篇关于难以访问在 Interface Builder .xib 文件中创建的 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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