折叠UITableView

时间:2012-06-20 21:12:03

标签: iphone objective-c ios uitableview

嘿大家我在我的应用程序中创建了一个UITableView,当一个单元格被触摸时,它扩展了我遇到的问题是它不会崩溃无论我尝试过什么,我确定它很简单我只是想弄清楚它它崩溃的唯一时间是它被轻敲另一个细胞。

这是我的代码:

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

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

    if (selectedCellIndexPath) {
        selected = YES;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{        
    if(selectedCellIndexPath != nil  
       && [selectedCellIndexPath compare:indexPath] == NSOrderedSame)  
        return 150;

    return 44;
}

4 个答案:

答案 0 :(得分:1)

您永远不会将selectedCellIndexPath更改为nil,因此在选择新的行之前,不会更改当前所选行。在didSelectRowAtIndexPath中,您应该将开头更改为以下内容:

if (selectedCellIndexPath == indexPath)
  selectedCellIndexPath = nil;
else
  selectedCellIndexPath = indexPath;

答案 1 :(得分:0)

In selectedCellIndexPath you are storing pointer of indexPath. It can change when you reload table, means indexPath object of same cell can be different when you select it second time.
It's safer if you store indexPath.section & indexPath.row 



      -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {    
               if(_section == indexPath.section && _row == indexPath.row)
               {
                  _section = -1;
                  _row = -1
               }
               else
               {
                  _section = indexPath.section;
                  _row = indexPath.row;
               }   
               [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

                if (selectedCellIndexPath) {//change it as per your requirement
                    selected = YES;
                }
         }

      -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
        {        
               if(_section == indexPath.section && _row == indexPath.row)  
                   return 150;

               return 44;
        }

答案 2 :(得分:0)

我创建了折叠UITableView

https://github.com/floriankrueger/iOS-Examples--UITableView-Combo-Box/zipball/master http://www.codeproject.com/Articles/240435/Reusable-collapsable-table-view-for-iOS

https://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html

它工作得很好..

答案 3 :(得分:0)

调用行重载函数时是否输入了cellForRow委托函数?如果确实如此,那么在检查所选行之后,您应该放置一些功能来折叠行。