[原]列表的二級展開功能。在原生UITableView上擴充實現。,uitableview

來源:互聯網
上載者:User

[原]列表的二級展開功能。在原生UITableView上擴充實現。,uitableview
  還是直接貼代碼了。

第一步:

@interface TodoViewController (){    //聲明一個數組 存放cell的資訊    NSMutableArray *grouparr;}@end

 

第二步:

//將cell的狀態存入數組中-(void)initDataSource{    NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"TodoCell",@"cell",@"NO",@"state",nil];    NSMutableDictionary *nameAndStateDic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"TodoCell",@"cell",@"NO",@"state",nil];    NSMutableDictionary *nameAndStateDic3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"TodoCell",@"cell",@"NO",@"state",nil];    NSMutableDictionary *nameAndStateDic4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"TodoCell",@"cell",@"NO",@"state",nil];    grouparr = [[NSMutableArray alloc] initWithObjects:nameAndStateDic1,nameAndStateDic2,nameAndStateDic3, nameAndStateDic4,nil];}

  

第三步:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return grouparr.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    if ([grouparr[indexPath.row][@"cell"] isEqualToString:@"TodoCell"]) {                static NSString *cellID = @"cell";        TodoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];        if (!cell) {            cell = [[TodoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];                    }        NSData *data = dataArray[indexPath.row];         [cell bindData:data];                return cell;    }    else {                static NSString *CellIdentifier = @"AttachedCell";                AttachedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];                if (cell == nil) {            cell = [[AttachedCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];                        cell.selectionStyle = UITableViewCellSelectionStyleNone;        }        return cell;    }        return nil;    }

  

第四步:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    //點擊cell後 改變cell的顏色 漸層    [_tableView deselectRowAtIndexPath:indexPath animated:YES];        NSIndexPath *path = nil;    if ([grouparr[indexPath.row][@"cell"] isEqualToString:@"MainCell"])    {        if ([grouparr[indexPath.row][@"cell"] isEqualToString:@"MainCell"]) {            path = [NSIndexPath indexPathForItem:(indexPath.row+1) inSection:indexPath.section];        }        else if ([grouparr[indexPath.row][@"cell"] isEqualToString:@"AttachedCell"])        {            path = indexPath;        }            NSLog(@"現在是第%ld行",indexPath.row);            if ([grouparr[indexPath.row][@"state"] boolValue] ) {            // 關閉附加cell            NSMutableDictionary *dd = grouparr[indexPath.row];            NSString *name = dd[@"name"];            NSMutableDictionary *nameAndStateDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",name,@"name",@"NO",@"state",nil];            grouparr[(path.row-1)] = nameAndStateDic;            [grouparr removeObjectAtIndex:path.row];            NSLog(@"MainCell's grouparr:%@",grouparr);            [_tableView beginUpdates];            [_tableView deleteRowsAtIndexPaths:@[path]  withRowAnimation:UITableViewRowAnimationMiddle];            [_tableView endUpdates];        }        else        {            // 開啟附加cell            NSMutableDictionary *dd = grouparr[indexPath.row];            NSString *name = dd[@"name"];                    NSMutableDictionary *nameAndStateDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MainCell",@"cell",name,@"name",@"YES",@"state",nil];            grouparr[(path.row-1)] = nameAndStateDic;                    NSMutableDictionary *nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"AttachedCell",@"cell",@"YES",@"state",nil];                    [grouparr insertObject:nameAndStateDic1 atIndex:path.row];            NSLog(@"AttachedCell's grouparr:%@",grouparr);            [_tableView beginUpdates];            [_tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];            [_tableView endUpdates];        }    }}

  

搞定收工!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.