1

嘿大家我在我的应用程序中制作了一个 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

4 回答 4

1

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

if (selectedCellIndexPath == indexPath)
  selectedCellIndexPath = nil;
else
  selectedCellIndexPath = indexPath;
于 2012-06-20T21:17:44.920 回答
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;
        }
于 2012-06-21T05:03:06.683 回答
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

它工作得很好..

于 2012-06-21T05:12:28.043 回答
0

当您调用行重载函数时,它是否进入 cellForRow 委托函数?如果是这样,那么您应该在检查所选行后添加一些功能来折叠行。

于 2012-06-21T05:24:09.957 回答