0

我正在为我的表格视图使用自定义单元格。在我的单元格中,我有图像视图和按钮。我在单行中显示 3 张图像。当我选择 button 时,我使用的是复选框图像,当再次点击 button 时,它会取消选择复选框图像。当我选择第一行中的按钮并滚动 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;
}

有时隐藏按钮和图像视图的逻辑也会变得混乱。即使数据可用,它也会隐藏图像视图和按钮。

非常感谢任何帮助。谢谢

4

1 回答 1

0

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

refreshCellView:(NSDictionary)数据

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

于 2012-06-14T12:08:45.123 回答