嵌套的 UITableView 不返回任何单元格 [英] Nested UITableView not returning any cells

查看:18
本文介绍了嵌套的 UITableView 不返回任何单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView,它有另一个 UITableView 嵌套在它的一个单元格内(我知道这是不好的做法,别担心!).

I have a UITableView which has another UITableView nested inside one its cells (I know this is bad practise, don't worry!).

问题是,当我调用 dequeueReusableCellWithIdentifier: 时,我得到了零.然而,当 UITableView 没有嵌套在另一个中时,这工作得很好.

The problem is that when I call dequeueReusableCellWithIdentifier: I am getting nil back. HOWEVER this works just fine when the UITableView is not nested inside another one.

有没有办法不重复使用 UITableViewCell,而是每次都直接实例化它?

Is there a way to NOT reuse a UITableViewCell, but instead directly instatiate it every time?

我试过用这个:

ContactFieldCell *cell = [[ContactFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:thisCellIdentifier];

它不返回 nil,但是在我的 UITableView 中什么也没有出现!

which doesn't return nil, but then nothing appears in my UITableView!

这是父"UITableView 的代码:

Here's the code for the "parent" UITableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ContactCardCell";
    ContactCardCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSArray *objects = [[sections objectAtIndex:indexPath.section] objectForKey:@"objects"];
    CDCard *card = [objects objectAtIndex:indexPath.row];

    cell.delegate = self;

    cell.fieldsTableView = [[CardTableViewController alloc] initWithCard:card];
    [cell.fieldsTableView.view setFrame:CGRectMake(17, 12, 256, 163)];
    [cell.contentView addSubview:cell.fieldsTableView.view];

    return cell;
}

这里是子"UITableView 的代码:

and here's the code for the "child" UITableView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *thisCellIdentifier = @"ContactFieldCell";

    ContactFieldCell *cell = [self.tableView dequeueReusableCellWithIdentifier:thisCellIdentifier];

    cell.delegate = self;
    cell.field = [self.card.sortedFields objectAtIndex:indexPath.row];

    return cell;
}

ContactFieldCell 是故事板中的原型单元.它有以下代码:

ContactFieldCell is a prototype cell within the storyboard. It has the following code:

@interface ContactFieldCell : UITableViewCell

@property (nonatomic, weak) id<ContactFieldCellDelegate> delegate;
@property (nonatomic, strong) CDField *field;

@property (nonatomic, strong) IBOutlet UILabel *displayNameLabel;

@end

推荐答案

UITableView 是一个非常强大的元素,可用于构建出色的应用程序.

UITableViews are a very powerful element and can be used to build great apps.

唯一要记住的是,基础知识必须清楚.现在从您的代码中,我无法确定您是否正确分配了 delegates 和 dataSources,但我仍然会提及它,以防其他人需要它.

The only thing to keep in mind is, the basics must be clear. Now from your code, I cannot make out whether you have assigned the delegates and dataSources properly, but I'll still mention it in case someone else needs it.

您有一个子类 UITableViewCell,它又包含一个 UITableView.UIViewController 必须是外部 UITableViewdelegatedataSource.确保您已在 .h.m 文件中进行了设置.

You have a subclassed UITableViewCell which in turn contains a UITableView. The UIViewController must be the delegate and dataSource for the outer UITableView. Make sure you have set it in both the .h and .m file.

接下来,您的自定义单元格也必须是 delegatedataSource,但对于内部 UITablewView.我想在这里,您已经在 UITableViewCellinit 方法中创建了内部 UITableView.在那里设置 delegatedataSource 本身.然后在 drawRect 方法中设置其他运行时属性(如果需要)并调用它的 reloadData.

Next, your custom cell must also be the delegate and dataSource, but for the inner UITablewView. I suppose here, you have created the inner UITableView in the init method of the UITableViewCell. Set the delegate and dataSource there itself. Then you set other runtime properties in the drawRect method (if needed) and call it's reloadData.

UIViewController 必须覆盖外部表的委托和数据源方法,单元格必须覆盖内部表的方法.

The UIViewController must override the delegate and dataSource methods for the outer table and the cell must override the methods for the inner table.

另外,请确保在绘制单元格时,您的数据不是 nilnull.

Also, make sure, the time the cells are plotted, your data is not nil or null.

还有一个很重要的事实,人们会错过以下代码:

And a very important fact, that people miss is the following code:

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

仅仅使单元出列是不够的.单元格第一次出列时,它是 nil 因为它还没有被创建.因此 if 条件.一旦它被分配和初始化并添加到表中,出队代码就会在此后工作.

Just dequeueing the cell is not enough. The first time a cell is dequeued, it is nil because it has not been created yet. Hence the if condition. Once it is allocated and initialized and added to the table, the dequeue code works thereafter.

注意 :在更仔细地查看您的代码后(抱歉没有第一次查看),我注意到您已经为您的单元分配了一个 UITableViewController.您认为单元格将如何显示控制器?使用 UITableView 代替.尝试遵循我在第 3 段中提到的模式.使用自定义单元格中的表格作为私有成员(或属性,您的意愿),在 init 中分配它.将数据从视图控制器分配给单元格.然后使用这些数据在 drawRect 中设置内部表格视图单元格的属性.它应该可以正常工作.

NOTE : After looking more closely to your code (sorry for not looking the first time), I noticed you have allocated a UITableViewController to your cell. How do you think the cell is going to display a controller? Use a UITableView instead. Try to follow the pattern I have mentioned in paragraph 3. Use a table in the custom cell as a private member (or property, your wish), allocate it in init. Assign the data to the cell from your view controller. Then use this data to set the inner table view cell's properties in it's drawRect. It should work fine.

这篇关于嵌套的 UITableView 不返回任何单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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