自定义 UITableViewCell 在滚动时更改索引路径?
我有一个在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在 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.