生成此问题是因为您的UITableView
重复使用单元格创建新单元格。 我给你一些可能对你有所帮助的建议。
1)
之间
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
/// Your code;
}
return cell;
}
1)你的UIView添加到cell.contentView.
EDITED添加UIView
:
你按照我的编辑答案之前,我想告诉你,下面的代码对内存管理不利,因为它会为每行UITableView
创建一个新的单元,所以要小心。
但是如果UITableView
有限制的行(50-100可能是)使用下面的代码更好,如果它适合你。
NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
}
请在这里添加您的相关代码:) – iPatel
您是从一个笔尖或故事板加载单元,还是完全使用代码创建它?你在使用'UITableViewCell'的自定义子类吗? –
@robmayoff我为这个单元使用一个xib并从xib载入它。我也为这个 – Lukas