国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-14 19:32:27
字體:
來源:轉載
供稿:網友

  還是直接貼代碼了。

第一步:

@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];        }    }}

  

搞定收工!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万山特区| 洛浦县| 阳新县| 宝坻区| 潜江市| 台湾省| 呼和浩特市| 中牟县| 浑源县| 老河口市| 常德市| 九龙坡区| 绥芬河市| 华蓥市| 兴隆县| 图们市| 文山县| 会宁县| 荣成市| 宜兰市| 龙岩市| 新干县| 密云县| 台湾省| 甘孜县| 三门峡市| 保康县| 兴安盟| 定边县| 蒙阴县| 镇巴县| 泰兴市| 昭通市| 新巴尔虎右旗| 溧水县| 丽水市| 霞浦县| 东兴市| 皮山县| 富顺县| 突泉县|