OGeek|极客世界-中国程序员成长平台

标题: ios - UITableView 展开和折叠以显示部分或全部单元格 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 14:07
标题: ios - UITableView 展开和折叠以显示部分或全部单元格

我有 UITableView,它使用一个高约 200 像素的 commentTableCell,它同时包含文本和图像。

我希望当未选择一行时,该行的高度约为 65 像素,这将隐藏所有图像,当该行扩展到 200 像素时,commentTableCell 将完全展开,因此内部的图像细胞会暴露。

但是我遇到的问题是,当所有行都折叠到 65 像素的高度时,commentTabelCell 中的任何图像都不应该显示,但是我从表格的某些部分显示的单元格中得到了重影或部分图像.为什么?

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{            
        static NSString *CellIdentifier = @"customCell";

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

        return cell;    
}

-(CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
{
        if(selectedIndex == indexPath.row)
        {

            return 200;
        }
        else {

            return 65;
        }  
}

-(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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];
}



Best Answer-推荐答案


我有一个替代解决方案。试试这个:

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

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

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

// Set your default row height
- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath {  
    return 65;  
}  

// In the implementation file:  
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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;  
}  

关于ios - UITableView 展开和折叠以显示部分或全部单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11005106/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4