问题描述
我在视图中有2个UITableView,仅在第二个表的第5行和第7行添加了UITableViewCellAccessoryDisclosureIndicator。
I'm having 2 UITableView in a view and I added the UITableViewCellAccessoryDisclosureIndicator only at the 2nd table row 5 and row 7.
但是向下滚动第二个表后(第1行消失),然后滚动回到顶部(出现第1行),第1行现在具有UITableViewCellAccessoryDisclosureIndicator ?!第1行以某种方式变为第5行或第7行吗???下面是我的cellForRowAtIndexPath代码:
But after scrolling the 2nd table down (which row 1 disappears) and then scroll back to top (which row 1 appears), row 1 now has the UITableViewCellAccessoryDisclosureIndicator?! Did row 1 somehow become row 5 or row 7??? Below is my code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.textColor = [UIColor blueColor];
cell.detailTextLabel.textColor = [UIColor blackColor];
if (tableView == table1)
{
cell.textLabel.text = [title1 objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [list1 objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else if (tableView == table2)
{
cell.textLabel.text = [title2 objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [list2 objectAtIndex:indexPath.row];
if (indexPath.row == 5 || indexPath.row == 7)
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
return cell;
}
非常感谢!
推荐答案
UITableViewCells被重新使用以优化性能。这发生在 [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
您需要在每次调用 tableView:(UITableView时,在单元格上显式设置您想要的任何属性*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
。
UITableViewCells are reused to optimize performance. This happens in [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
You need to explicitly set any properties you would like on the cell at each call of tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
.
类似这样的方法可以解决问题:
Something like this should resolve the issue:
if (indexPath.row == 5 || indexPath.row == 7)
{
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else
{
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
}
这篇关于滚动后,iOS UITableView Cell是否正确加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!