首页 > 解决方案 > 如何在 UITableView 中创建行的展开和折叠

问题描述

如何在 UITableView 中创建行的展开和折叠,

单击单元格后,我关注了此视频“ https://www.youtube.com/watch?v=OYaI5ASpsOE ”,出现以下错误。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to compare <NSIndexPath: 0x600000029560> {length = 2, path = 0 - 18446744073709551615} with object of different class: NSArray'

标签: iosobjective-cuitableview

解决方案


在我使用崩溃的 tableview 委托和数据源代码下面对我来说是不可重现的

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.arrowtitle.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ExpandingCell *cell = (ExpandingCell *)[tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"Expanding Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.lblrow.text = self.arrow[indexPath.row];
    cell.lblrowtitle.text = self.arrowtitle[indexPath.row];
    cell.lblfruit.text = self.arrFruit[indexPath.row];
    NSInteger cal = (indexPath.row) * 25;
    cell.lblcalcal.text = [NSString stringWithFormat:@"%ld",(long) cal];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (self.selectedIndex == indexPath.row) ? 120  : 45 ;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectedIndex == indexPath.row) {
        self.selectedIndex = -1;
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        return;
    }
    if (self.selectedIndex == -1) {
        NSIndexPath *prev = [NSIndexPath indexPathForRow:self.selectedIndex inSection:0];
        self.selectedIndex = indexPath.row;
        [tableView reloadRowsAtIndexPaths:@[prev] withRowAnimation:UITableViewRowAnimationFade];
    }
    self.selectedIndex = indexPath.row;

    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

推荐阅读