Cell Reusable导致iOS5中的重复数据

时间:2012-06-14 10:38:30

标签: ios5 uitableview reuseidentifier

我在桌面视图中使用自定义单元格。在我的手机中,我有imageview&按钮。我在单行显示3张图片。当我选择按钮时,我使用的是复选框图像&当再次点击按钮时,它取消选中复选框图像。当我选择第一行和按钮时的按钮。滚动tabelview,它还会检查第3行或第4行的按钮。我认为这是由于细胞的重新使用。这是我的cellForRowAtIndexPath代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"LibraryElementsCell";
    LibraryElementsCell *cell = (LibraryElementsCell *) [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        NSArray *cellArray=[[NSBundle mainBundle] loadNibNamed:@"LibraryElementsCell" owner:self options:nil];
        cell=[cellArray objectAtIndex:0];

        [cell.firstElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
        [cell.firstElementButton addTarget:self action:@selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];

        [cell.secondElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
        [cell.secondElementButton addTarget:self action:@selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];

        [cell.thirdElementButton setImage:[UIImage imageNamed:CHECKBOX_UNCHECKED_IMAGE] forState:UIControlStateNormal];
        [cell.thirdElementButton addTarget:self action:@selector(checkBoxSelectedOnLibraryElement:event:) forControlEvents:UIControlEventTouchUpInside];

        tableView1.backgroundColor = [UIColor clearColor];
        tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSMutableArray *tempArray = [self.categoriesArray objectAtIndex:indexPath.section];

    int row = indexPath.row * 3;

    if (row <= [tempArray count]) 
    {
        LibraryElement *libElement = [tempArray objectAtIndex:row];
        cell.firstElementImageView.image = [UIImage imageNamed:libElement.imageName];
    }

    if ((row + 1) < [tempArray count]) 
    {
        LibraryElement *libElement = [tempArray objectAtIndex:row+1];
        cell.secondElementImageView.image = [UIImage imageNamed:libElement.imageName];
    }
    else {
        [cell.secondElementImageView setHidden:YES];
        [cell.secondElementButton setHidden:YES];
    }

    if ((row + 2) < [tempArray count]) 
    {
        LibraryElement *libElement = [tempArray objectAtIndex:row+2];
        cell.thirdElementImageView.image = [UIImage imageNamed:libElement.imageName];
    }
    else {
        [cell.thirdElementImageView setHidden:YES];
        [cell.thirdElementButton setHidden:YES];
    }

    return cell;
}

有时也会隐藏按钮和按键的逻辑。 imageview得到了认可。它隐藏了imageview&amp;即使数据可用,按钮也是如此。

非常感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:0)

每次都需要刷新单元组件 例如在单元格内创建一个刷新方法,用于获取数据并绘制单元格组件

  

refreshCellView:(NSDictionary的)数据

并在cellForRowAtIndexPath内调用refreshCellView:获取单元格后