This article shares the specific code for iOS to implement cell folding for your reference. The specific content is as follows
The core of the collapse is the number of rows or columns of the cell to change in real time
The more important steps are:
1. Set an array (variable array, used to update cell content)
2. Call method - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { (to get the selected cell)
For example, we define this tableView as littletableView
NSIndexPath *row = [ indexPathForSelectedRow];
The above code is used to obtain the indexPath of the selected cell.
3. How to exchange the selected cell content with the first cell content
For example, we have an array now
1 2 3 4 5
We selected 4
Use 4 to [array insertObject: array [] atIndex:0]; this method mentions the first one of 1
It becomes 4 1 2 3 4 5, and the index of the second 4 is added 1
When we delete [array removeObjectAtIndex:+1]; the index needs to be added by 1
4. Change the size of the tableView (This is a very important point. If your tableView frame is not enough, even if you set 10 columns and 10 rows for it, it will not change, because once it cannot be accommodated, the method that defines the cell will not be executed (personal test)
Here is how to implement it step by step
1. In the .h file of the view controller, set two UItableView properties and a boolean value to determine whether the cell is expanded, and set a variable array to save the cell's title information.
2. Initialize them in viewDidLoad to set the proxy and set the tag value
When implementing the protocol method, according to different tag values, different method bodies are corresponding to:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if ( == 101) { return 1; } else { if (cellisOpen == NO) { return 1; } else { return 4; } } }
//Note that no proxy is set, these methods will not be executed
3. When there is no expansion, the number of rows in the small cell is 1 (we assume that there is only one group, and the number of rows changes), so
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if ( == 101) { return 1; } else { return 4; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if ( == 101) { return 1; } else { if (cellisOpen == NO) { return 1; } else { return 4; } } }
In the function of creating a cell, you also need to distinguish between the different situations of tag and boolean variables.
It should be noted that when creating, there is no expanded cell that keeps displaying the first element of the array
4. Next is the last very critical function
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"Enter this function"); NSIndexPath *row = [ indexPathForSelectedRow]; [arrayrows insertObject:arrayrows[] atIndex:0]; //arrayrows is an array that stores cell information NSLog(@"%@",arrayrows[]); // Let's print the selected cell [arrayrows removeObjectAtIndex:+1]; // Add 1 to the selected cell index when deletion for (int i = 0; i < 4; i++) { NSLog(@"%@",arrayrows[i]); } if ( == 102) { if (cellisOpen == NO) { cellisOpen = YES; //Change the Boolean variable for fold judgment } else { cellisOpen = NO;//Change the Boolean variable for fold judgment } } if (cellisOpen == YES) { = CGRectMake(280, 85, 120, 160); // Change the cell size } else { = CGRectMake(280, 85, 120, 40); } NSLog(@"%d",cellisOpen); //Print the boolean value at this time [littletableView reloadData]; // Update the information of the clicked cell
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.