The secondary expansion function of the [original] list. Extends the implementation on native UITableView ., Uitableview

Source: Internet
Author: User

The secondary expansion function of the [original] list. Extends the implementation on native UITableView ., Uitableview
Or directly paste the code.

Step 1:

@ Interface TodoViewController () {// declare an array to store cell information NSMutableArray * grouparr;} @ end

 

Step 2:

// Save the cell status to the array-(void) initDataSource {NSMutableDictionary * nameAndStateDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @ "TodoCell", @ "cell", @ "NO ", @ "state", nil]; NSMutableDictionary * nameAndStateDic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @ "TodoCell", @ "cell", @ "NO", @ "state", nil] NSMutableDictionary * nameAndStateDic3 = [NSMutableDictionary example: @ "TodoCell", @ "cell", @ "NO", @ "state", nil]; NSMutableDictionary * nameAndStateDic4 = [response failed: @ "TodoCell", @ "cell", @ "NO", @ "state", nil]; grouparr = [NSMutableArray alloc] handler: nameAndStateDic1, nameAndStateDic2, nameAndStateDic3, nameAndStateDic4, nil];}

  

Step 3:

-(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;    }

  

Step 4:

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {// click cell to change the cell color gradient [_ tableView deselectRowAtIndexPath: indexPath animated: YES]; NSIndexPath * path = nil; if ([grouparr [indexPath. row] [@ "cell"] isw.tostring: @ "MainCell"]) {if ([grouparr [indexPath. row] [@ "cell"] isw.tostring: @ "MainCell"]) {path = [NSIndexPath indexPathForItem :( indexPath. row + 1) inSection: indexPath. section];} else if ([grouparr [indexPath. row] [@ "cell"] isw.tostring: @ "AttachedCell"]) {path = indexPath;} NSLog (@ "the current row is % ld", indexPath. row); if ([grouparr [indexPath. row] [@ "state"] boolValue]) {// close the additional 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: parent]; [_ tableView endUpdates];} else {// open the additional 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 Syntax: @ "AttachedCell", @ "cell", @ "YES", @ "state", nil]; [grouparr insertObject: nameAndStateDic1 atIndex: path. row]; NSLog (@ "AttachedCell's grouparr: % @", grouparr); [_ tableView beginUpdates]; [_ tableView insertRowsAtIndexPaths: @ [path] withRowAnimation: parent]; [_ tableView endUpdates] ;}}

  

Work done!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.