自定义 UITableViewCell 在滚动时更改索引路径?

发布于 2024-08-30 07:09:23 字数 1036 浏览 3 评论 0原文

我有一个在 Interface Builder 中创建的自定义 UITableViewCell。我成功地将单元格出队,但当我滚动时,单元格似乎开始调用不同的索引路径。在此示例中,我将当前的indexPath.section 和indexPath.row 输入到customCellLabel 中。当我上下滚动表格时,一些单元格会发生变化。数字可以遍布各处,但单元格在视觉上不会跳跃。

如果我注释掉 if(cell==nil),那么问题就消失了。

如果我使用标准电池,问题就会消失。

为什么会发生这种情况?

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CalendarEventCell"];
    if (cell == nil) {
        NSLog(@"Creating New Cell !!!!!");
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


   // Set up the cell...
  [customCellLabel setText:[NSString stringWithFormat:@"%d - %d",indexPath.section, indexPath.row]];


    return cell;
}

I have a custom UITableViewCell which I created in Interface Builder. I am successfully Dequeuing cells, but as I scroll, the cells appear to begin calling different indexPaths. In this example, I am feeding the current indexPath.section and indexPath.row into the customCellLabel. As I scroll the table up and down, some of the cells will change. The numbers can be all over the place, but the cells are not skipping around visually.

If I comment out the if(cell==nil), then the problem goes away.

If I use a standard cell, the problem goes away.

Ideas why this might be happening?

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CalendarEventCell"];
    if (cell == nil) {
        NSLog(@"Creating New Cell !!!!!");
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


   // Set up the cell...
  [customCellLabel setText:[NSString stringWithFormat:@"%d - %d",indexPath.section, indexPath.row]];


    return cell;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拒绝两难 2024-09-06 07:09:23

当您在 if(cell==nil) 条件下创建单元格时,是否将reuseIdentifier 分配给该单元格作为“CalendarEventCell”?我可以看到您的笔尖名称是 CalendarEventCell,但我想您还需要设置

cell.reuseIdentifier = @"CalendarEventCell";

如果没有,那么我不确定它是否可以将正确的单元格出队。另外,不确定 customCellLabel 指的是什么。

When you create a cell in if(cell==nil) condition, are you assigning a reuseIdentifier to the cell as "CalendarEventCell"? I can see your nib name is CalendarEventCell, but I guess you would also need to set

cell.reuseIdentifier = @"CalendarEventCell";

If not, then I am not sure if it can deque the correct cells. Also, not sure what customCellLabel refers to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文