Dan*_*any 2 iphone objective-c uitableview
我在表视图中有一个单元格列表,其中只有最后一个单元格有一个披露指示符(它触发一个动作).当我向下滚动单元格列表时,一切正常,但如果我奇怪地向上滚动,披露指示符也会出现在其他单元格中.我无法弄清楚问题出在哪里,有什么帮助吗?
谢谢,Daniele
这是使用的代码的一部分:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
if(([myArray count]-1) == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
Run Code Online (Sandbox Code Playgroud)
这不是一个错误:它是一个特征;)因为细胞正在被重用.
如果你的tableview包含200个单元格而你的iphone能够同时显示5个单元格,那么你只有5-6个实例UITableViewCell
.如果你向下滚动一个单元格获取披露按钮,如果你向后滚动单元格正在重复使用,结果披露按钮仍然存在.
解决你的问题:
方法1:不仅在最后一个单元格上设置公开按钮.你还应该在其他单元格中删除/取消它.
方法2:似乎最后一个细胞是语义上的另一种细胞.因此,请选择重用标识符,例如:@"MyLastCell
最后一个单元格和@"MyCell"
所有其他单元格.因此,您的tableview将只重用相同类型的单元格(在您的情况下:有/没有公开按钮)
编辑1:方法2的一些样本伪代码; 编辑3:方法2的更短解决方案
static NSString *CellIdentifier = @"Cell";
static NSString *LastCellIdentifier = @"LastCell";
bool isLastRow = (indexPath.row == numRows-1);
NSString *usedCellIdentifier = isLastRow ? LastCellIdentifier : CellIdentifier;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: usedCellIdentifier];
if(!cell)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:usedCellIdentifier] autorelease];
if(isLastCell)
{
//do disclosure-stuff here. Or add a UIButton here
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2:方法1的示例代码
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = (idexPath.row == numRows-1) ?
UITableViewCellAccessoryDetailDisclosureButton
: UITableViewCellAccessoryNone ;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2932 次 |
最近记录: |