索引路径混淆行的单元格

时间:2014-10-01 16:14:46

标签: ios uitableview nsmutablearray

我有一个显示字符串帖子或图像帖子的单元格但是当我将另一个对象添加到包含单元格数据的数组时,图像帖子中的图像会复制到tableView上。我认为我在cellForRowAtIndex路径方法中做错了什么:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
postObject *currentPost = [self.postArray objectAtIndex:indexPath.row];
self.cell = (storyboardCell *) [self.tableView dequeueReusableCellWithIdentifier:@"cell"];

if (currentPost.imagePost == nil) {
    _cell.personStringPost.text = currentPost.postString;
}
if (currentPost.imagePost != nil) {
    _cell.personImagePost.image = currentPost.imagePost;
    _cell.personStringPost.hidden = true;
}
[self changeToCircle:_cell.profileImage];
_cell.profileImage.image = currentPost.profileImage;

[_cell.nameLabel setTitle:currentPost.nameString forState:UIControlStateNormal];
[_cell.eventLabel setTitle:currentPost.eventString forState:UIControlStateNormal];

[_cell.profileImageButton addTarget:self action:@selector(profilePage) forControlEvents:UIControlEventTouchUpInside];
[_cell.nameLabel addTarget:self action:@selector(profilePage) forControlEvents:UIControlEventTouchUpInside];
[_cell.eventLabel addTarget:self action:@selector(eventPage) forControlEvents:UIControlEventTouchUpInside];
[_cell.likePage addTarget:self action:@selector(likePage) forControlEvents:UIControlEventTouchUpInside];
[_cell.comment addTarget:self action:@selector(commentPage) forControlEvents:UIControlEventTouchUpInside];
_cell.selectionStyle = UITableViewCellSelectionStyleNone;

return _cell;

}

2 个答案:

答案 0 :(得分:0)

我怀疑这是由于细胞重用。滚动时,tableView会保留屏幕上行的单元格,并在调用dequeueReusableCellWithIdentifier:时重复使用它们。如果他们之前有图像,它仍然会存在。如果是这样,您需要将其删除。同样,如果之前隐藏了personStringPost,它仍然会被隐藏。我也会避免使用self.cell或_cell;声明一个局部变量。修改您的代码如下:

postObject *currentPost = [self.postArray objectAtIndex:indexPath.row];
StoryboardCell *cell = (storyboardCell *) [self.tableView dequeueReusableCellWithIdentifier:@"cell"];

if (currentPost.imagePost == nil) {
    cell.personStringPost.text = currentPost.postString;
    cell.personStringPost.hidden = false;
    cell.personImagePost.image = nil;
}
if (currentPost.imagePost != nil) {
    cell.personImagePost.image = currentPost.imagePost;
    cell.personStringPost.hidden = true;
    cell.personStringPost.text = @""; 
}
[self changeToCircle:cell.profileImage];
cell.profileImage.image = currentPost.profileImage;

[cell.nameLabel setTitle:currentPost.nameString forState:UIControlStateNormal];
[cell.eventLabel setTitle:currentPost.eventString forState:UIControlStateNormal];

[cell.profileImageButton addTarget:self action:@selector(profilePage) forControlEvents:UIControlEventTouchUpInside];
[cell.nameLabel addTarget:self action:@selector(profilePage) forControlEvents:UIControlEventTouchUpInside];
[cell.eventLabel addTarget:self action:@selector(eventPage) forControlEvents:UIControlEventTouchUpInside];
[cell.likePage addTarget:self action:@selector(likePage) forControlEvents:UIControlEventTouchUpInside];
[cell.comment addTarget:self action:@selector(commentPage) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;

答案 1 :(得分:0)

我不确定,但你应该尝试以下代码。

if(currentPost.imagePost == nil){

     _cell.personStringPost.text = currentPost.postString;
     _cell.personStringPost.hidden = FALSE;

}其他{

     _cell.personStringPost.hidden = TRUE;

}

if(currentPost.imagePost!= nil){

     _cell.personImagePost.image = currentPost.imagePost;
     _cell.personImagePost.hidden = false;

}其他{

     _cell.personImagePost.image = nil;
     _cell.personImagePost.hidden = true;

}