滚动 UITableView 时单元格消失并重新出现
Cells vanishing and reappearing when scrolling a UITableView
我的 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;
}
这可能是因为我对每个小区都有相同的小区重用标识符吗?它需要是唯一的吗?
不,对于 tableView,单元格重用标识符需要 常量。但是,您应该将 is 用作静态变量。
更改为 static NSString *cellID = [NSString stringWithFormat:@"cellID",indexPath.row];
而不是
NSString *cellID = [NSString stringWithFormat:@"cell%d",indexPath.row];
您还对 willDisplayCell
或 didEndDisplayingCell
委托方法进行了任何更改。如果是,Plz post 那个代码也是。
在此代码中:
UIButton *button = [[UIButton alloc] init];
button = self.buttons[i];
您分配并初始化一个按钮,然后将该变量重新分配给按钮数组的第 i 个条目。这是否意味着其他东西?
您还将按钮作为子视图添加到单元格内容中。滚动时会重复使用单元格,因此您可以轻松地向给定单元格添加 4 个以上的按钮,这将导致显示问题。当您出列一个单元格时,它可能已经有 4 个按钮。
你能不能在单元格原型中只使用 4 个按钮,这样可以省去所有这些代码工作。
重新写成这样:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if (cell.contentView.subviews.count == 0) {
int x,y;
x=5;
y=0;
for (int i = 0; i < 4; i++) {
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;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
return cell;
}
}
我的 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;
}
这可能是因为我对每个小区都有相同的小区重用标识符吗?它需要是唯一的吗?
不,对于 tableView,单元格重用标识符需要 常量。但是,您应该将 is 用作静态变量。
更改为 static NSString *cellID = [NSString stringWithFormat:@"cellID",indexPath.row];
而不是
NSString *cellID = [NSString stringWithFormat:@"cell%d",indexPath.row];
您还对 willDisplayCell
或 didEndDisplayingCell
委托方法进行了任何更改。如果是,Plz post 那个代码也是。
在此代码中:
UIButton *button = [[UIButton alloc] init];
button = self.buttons[i];
您分配并初始化一个按钮,然后将该变量重新分配给按钮数组的第 i 个条目。这是否意味着其他东西?
您还将按钮作为子视图添加到单元格内容中。滚动时会重复使用单元格,因此您可以轻松地向给定单元格添加 4 个以上的按钮,这将导致显示问题。当您出列一个单元格时,它可能已经有 4 个按钮。
你能不能在单元格原型中只使用 4 个按钮,这样可以省去所有这些代码工作。
重新写成这样:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if (cell.contentView.subviews.count == 0) {
int x,y;
x=5;
y=0;
for (int i = 0; i < 4; i++) {
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;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
return cell;
}
}