2

高さ約200ピクセルのcommentTableCellを使用するUITableViewがあり、テキストと画像の両方があります。

行が選択されていない場合、行の高さは約65ピクセルで、すべての画像が非表示になり、行が200ピクセルに拡張されるとcommentTableCellが完全に拡張されるため、セル内の画像が取得されることを望んでいました露出。

しかし、私が抱えている問題は、すべての行が高さ 65 ピクセルに折りたたまれている場合、commentTabelCell の画像は表示されませんが、テーブルの一部に表示されているセルからゴーストまたは部分的な画像が表示されることです。なんで?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{            
        static NSString *CellIdentifier = @"customCell";

        CommentTableCell *cell = (CommentTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        return cell;    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if(selectedIndex == indexPath.row)
        {

            return 200;
        }
        else {

            return 65;
        }  
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

        //The user is selecting the cell which is currently expanded
        //we want to minimize it back
        if(selectedIndex == indexPath.row)
        {
            selectedIndex = -1;
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

            return;
        }

        //First we check if a cell is already expanded.
        //If it is we want to minimize make sure it is reloaded to minimize it back
        if(selectedIndex >= 0)
        {
            NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
            selectedIndex = indexPath.row;
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];        
        }

        //Finally set the selected index to the new selection and reload it to expand
        selectedIndex = indexPath.row;
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
4

1 に答える 1

3

別の解決策があります。これを試して:

// ----------------------------//

// Put this somewhere in your .h file:  
NSIndexPath *selectedCellIndexPath;  

// ----------------------------//

// Set your default row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
    return 65;  
}  

// In the implementation file:  
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
    selectedCellIndexPath = indexPath;  

   // Forces the table view to call heightForRowAtIndexPath  
   [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];  
}  

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
    // Note: Some operations like calling [tableView cellForRowAtIndexPath:indexPath]  
    // will call heightForRow and thus create a stack overflow  
    if(selectedCellIndexPath != nil && [selectedCellIndexPath compare:indexPath] == NSOrderedSame)  
        return 200;  

    return 65;  
}  
于 2012-06-13T00:50:45.120 に答える