在我的tableView中,在一些單元格中,我添加了一個imageView作爲單元格contentView的子視圖。在滾動tableView上下複製其他單元格上的這些圖像也。但是這個問題並不總是會發生。請提出解決方案..我使用下面的代碼。UITableViewCell contentView在滾動時複製子視圖(UIButton和UIImageView)
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (friendsArray.count != 0)
{
NSString *str = [friendsIdArray objectAtIndex:indexPath.row];
if ([pendingRequests containsObject:str])
{
// Add image for pending item
UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pendng.png"]];
pendImage.frame = CGRectMake(220.0, 2.5, 70, 40);
pendImage.tag = indexPath.row;
[cell.contentView addSubview:pendImage];
}
}
}
NSString *object;
if (friendsArray.count == 0)
{
if ([cell.contentView viewWithTag:indexPath.row])
{
for (UIView *subview in [cell.contentView subviews])
{
[subview removeFromSuperview];
}
}
object = @"No friends added to the list";
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
else
{
object = [friendsArray objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = UITextAlignmentLeft;
if (![cell.contentView viewWithTag:indexPath.row])
{
NSString *str = [friendsIdArray objectAtIndex:indexPath.row];
if ([pendingRequests containsObject:str])
{
// Add image for pending item
UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pendng.png"]];
pendImage.frame = CGRectMake(220.0, 2.5, 70, 40);
pendImage.tag = indexPath.row;
[cell.contentView addSubview:pendImage];
}
}
}
+1好試試。工作正常 –
@AnushaK,我試過這種方式,但沒有爲我工作,任何其他解決方案?如果是這樣,請回復 –
customTableViewCell * cell =(customTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 如果(cell == nil){UINib * nib = cell = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0]; } IM實際上做這種方式,我試圖以上烏爾解決方案,但是沒有幫助 –