2

我有 UITableView,它使用一个高约 200 像素的 commentTableCell,它既有文本又有图像。

我希望当未选择一行时,该行的高度约为 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 回答