我的UITableView有一些奇怪的行为。当我上下滚动时,有时下一行会消失并再次消失。不知道为什么会这样。我的每个UITableCells都包含4个按钮,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
int x,y;
x=5;
y=0;
for(int i = (int)indexPath.row*4; i<(int)indexPath.row*4+4; i++) {
//make sure we don't hit a blank in the array
if(i<[self.buttons count]) {
UIButton *button = [[UIButton alloc] init];
button = self.buttons[i];
button.layer.cornerRadius = button.frame.size.width / 2;
button.clipsToBounds = YES;
[button setFrame:CGRectMake(x, y, 70, 70)];
[cell.contentView addSubview:button];
x+=80;
}
}
return cell;
}
可能是因为每个单元都有相同的单元重用标识符吗?是否需要唯一?
在此代码中:
UIButton *button = [[UIButton alloc] init];
button = self.buttons[i];
您分配并初始化按钮,然后将变量重新分配给按钮数组的第i个条目。这是否意味着其他?
另外,您还将按钮作为子视图添加到单元格内容中。滚动时会重复使用单元格,因此您可以轻松地在给定的单元格中添加4个以上的按钮,这将导致显示问题。当您将一个单元出队时,它可能已经有4个按钮了。
您不仅可以在单元原型中拥有4个按钮,还可以节省代码中的所有工作量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句