IOS enables cell folding

Time:2021-7-31

This example shares the specific code of IOS to realize cell folding for your reference. The specific contents are as follows

The core of folding is that the number of rows or columns of a cell changes in real time

The more important steps are:

1. Set array (variable array, used to update cell content)

2. Call the method – (void) tableview: (uitableview *) tableview didselectrowatindexpath: (NSIndexPath *) indexpath {(to get the selected cell)
For example, we define this tableview as a little tableview
NSIndexPath *row = [self.littletableView indexPathForSelectedRow];
The above code is used to get the indexpath of the selected cell.

3. How to interchange the selected cell content with the first cell content

For example, we now have an array
1 2 3 4 5
We chose 4
Use [array insertobject: array [indexpath. Row] atindex: 0] for 4; This method is mentioned before 1
It becomes 4 1 2 3 4 5, and the index of the second 4 is increased by 1
When deleting [array removeobjectatindex: indexpath. Row + 1]; Add 1 to the index

4. Change the size of the tableview (this is a very important point. If the frame of your tableview 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 of defining the cell will not be executed (pro test)

Here’s how to do it step by step

1. In the. H file of the view controller, set the attributes of two uitableviews and a Boolean value to judge whether the cell is expanded, and set a variable array to save the title information of the cell.

2. Initialize the agent in viewdidload and set the tag value

When implementing the protocol method, different methods are corresponding according to different tag values, such as:


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 if (tableView.tag == 101) {
 return 1;
 } else {
 if (cellisOpen == NO) {
  return 1;
 } else {
  return 4;
 }
 }
}

//Note that if the proxy is not 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 (tableView.tag == 101) {
 return 1;
 } else {
 return 4;
 }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 if (tableView.tag == 101) {
 return 1;
 } else {
 if (cellisOpen == NO) {
  return 1;
 } else {
  return 4;
 }
 }
}

In the function of creating cells, you should also distinguish between tag and Boolean variables

It should be noted that when creating, the cell without expansion will always display the first element of the array

4. Next is the last key function

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Nslog (@ "enter this function");
 NSIndexPath *row = [self.littletableView indexPathForSelectedRow];
 [arrayrows insertObject:arrayrows[row.row] atIndex:0]; // Arrayrows is an array that stores cell information
 NSLog(@"%@",arrayrows[row.row]); // Let's print the selected cells
 [arrayrows removeObjectAtIndex:row.row+1]; //  Add 1 to the index of the selected cell when deleting
 for (int i = 0; i < 4; i++) {
 NSLog(@"%@",arrayrows[i]);
 }
 if (tableView.tag == 102) {
 if (cellisOpen == NO) {
 cellisOpen = YES; // Change the boolean variable of folding judgment
 } else {
  cellisOpen = NO;// Change the boolean variable of folding judgment
 }
 }
 if (cellisOpen == YES) {
 littletableView.frame = CGRectMake(280, 85, 120, 160); //  Change cell size
 } else {
 littletableView.frame = CGRectMake(280, 85, 120, 40);
 }
 NSLog(@"%d",cellisOpen); // Print Boolean values at this time
 [littletableView reloadData]; //  Update the clicked cell information

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.