【问题标题】:Problem with initiating UITableViewCells based on indexPath.row基于 indexPath.row 启动 UITableViewCells 的问题
【发布时间】:2009-04-06 19:21:51
【问题描述】:

我有以下方法应该用数组中的数据填充我的 UITableView 的单元格。我想使用正在加载数据的行作为索引从数组中获取数据。

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


    cellComments=(FullCommentCell *)[tableView dequeueReusableCellWithIdentifier:FullCommentCell_ID];
    if(cellComments==nil)
    {
        [[NSBundle mainBundle]loadNibNamed:@"FullCommentCell" owner:self options:nil];
        NSLog([NSString stringWithFormat:@"%i",indexPath.row]);
        [cellComments loadFullComments:[latestFMLComments objectAtIndex:indexPath.row]];
    }
    //cellComments.userInteractionEnabled=NO;
    return cellComments;

}

这没有按预期工作。该表最终只填充了我的数组的前三个元素,然后这些数据被重复使用,直到我的表结束。该表应该使用我数组中的所有数据。知道为什么这没有按预期工作吗?

【问题讨论】:

    标签: iphone objective-c cocoa cocoa-touch


    【解决方案1】:

    每次返回单元格时,您都需要设置正确的单元格数据,无论它是新的还是重复使用的。向下滚动时,表格顶部的单元格将被移除并重新用于表格底部。这就是为什么您会看到前几个数据项重复的原因。

    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        cellComments = (FullCommentCell *)[tableView dequeueReusableCellWithIdentifier:FullCommentCell_ID];
        if (cellComments == nil) {
            [[NSBundle mainBundle]loadNibNamed:@"FullCommentCell" owner:self options:nil];
    
            // Do any one-time setup here, like adding subviews
        }
    
        // Set cell data for both new and reused cells here
    
        [cellComments loadFullComments:[latestFMLComments objectAtIndex:indexPath.row]];
        //cellComments.userInteractionEnabled=NO;
    
        return cellComments;
    }
    

    【讨论】:

      【解决方案2】:

      当您从 dequeueReusableCellWithIdentifier 调用中获取 cellComments 时,您需要再次调用 loadFullCommnents - 单元格会被重复使用,因此您只会创建与屏幕上显示的一样多的单元格。

      【讨论】:

        猜你喜欢
        • 2012-01-05
        • 1970-01-01
        • 2017-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-29
        • 2011-04-03
        • 2021-01-07
        相关资源
        最近更新 更多