我正在尝试构建一个在每个单元格中包含一些长文本的UITableView。单元格没有AccessoryView,除了一个单元格(第8个),这是一种用于打开详细信息视图的按钮。
考虑下面的代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize size = [[quotes objectAtIndex:indexPath.row] sizeWithFont:16 constrainedToSize:CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX)];
return size.height+20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [quotes objectAtIndex:indexPath.row];
if(indexPath.row==7){
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
return cell;
}它可以工作,但问题是,当我滚动到表格的底部(第8行也是最后一行),然后我回到上面,另一个AccessoryView被添加到一个随机点(或多或少的第三个单元格,但我不知道它是在它里面还是随机浮动)。这与iOS的细胞重用有关吗?我怎么才能避免呢?
提前谢谢。
发布于 2012-09-17 08:32:55
您必须显式地为每个单元格设置公开按钮,但您希望公开的单元格除外。这样,当单元在其他地方被重用时,它的披露指示符就会被移除:
if(indexPath.row==7){
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}发布于 2012-09-17 08:32:21
单元正在被重用(正如您对-dequeueReusableCellWithIdentifer的调用所演示的那样)。
答案是在单元出队后将其设置为所需的默认值,或者向if语句添加一个else子句来处理它。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = [quotes objectAtIndex:indexPath.row];
// Set to expected default
[cell setAccessoryType:UITableViewCellAccessoryNone];
if(indexPath.row==7){
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
return cell;
}发布于 2012-09-17 08:34:35
正如您猜测的那样,这是由于单元重用。必须为索引路径不是7的单元格显式设置UITableViewCellAccessoryNone。
https://stackoverflow.com/questions/12451794
复制相似问题