0

テーブル ビューにカスタム セルを使用しています。私のセルには、イメージビューとボタンがあります。3 つの画像を 1 行に表示しています。ボタンを選択すると、チェックボックスの画像が使用され、再度ボタンをタップすると、チェックボックスの画像の選択が解除されます。最初の行のボタンを選択してタブビューをスクロールすると、3行目または4行目のボタンもチェックされます。これはセルのREUSEによるものだと思います。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;
}

また、ボタンとイメージビューの非表示ロジックが台無しになることもあります。データが利用可能であっても、画像ビューとボタンを非表示にします。

どんな助けでも大歓迎です.Thanks

4

1 に答える 1

0

毎回セルコンポーネントを更新する必要があります。たとえば、データを取得してセルコンポーネントを描画する更新メソッドをセル内に作成します。

refreshCellView:(NSDictionary)データ

そしてcellForRowAtIndexPath内でrefreshCellViewを呼び出します:セルを取得した後

于 2012-06-14T12:08:45.123 に答える