Implement QQ friends list in iOS TableView (3)
Next we fold the group clicks.
First, create a new variable dictionary to store whether the current list is displayed.
NSMutableArray * selectedArr; // determines whether the list is enabled.
selectedArr=[[NSMutableArray alloc]init];
According to the previous two sections, the group name is placed on the header of the section, but the Header does not have the same click method as the cell.
In this case, we can consider adding a button to the header to enable or disable the list when clicking.
-(UIView *) tableView :( UITableView *) tableView viewForHeaderInSection :( NSInteger) section {UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, SCREEN_WIDTH, 30)]; view. backgroundColor = [UIColor whiteColor]; UILabel * titleLabel = [[UILabel alloc] initWithFrame: CGRectMake (30, 5, SCREEN_WIDTH, 30)]; titleLabel. text = [titleArray objectAtIndex: section]; [view addSubview: titleLabel]; UIImageView * imageVi Ew = [[UIImageView alloc] initWithFrame: CGRectMake (5, 5, 20, 20)]; imageView. tag = 20000 + section; imageView. image = [UIImage imageNamed: @ "arrow_down.png"]; [view addSubview: imageView]; // Add a button to listen to the click group and close the group. UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom]; btn. frame = CGRectMake (0, 0, SCREEN_WIDTH, 50); btn. tag = 10000 + section; [btn addTarget: self action: @ selector (btnOpenList :) forControlEvents: UIControlEventTouchDown]; return view ;}
In this example, the tag of the button is set to 10000 + section to ensure that the tag of the button is used to identify which group is currently clicked (the tag is not directly set to section but is set to 10000 + section to ensure the left side you can also use tags to determine which group the image belongs, therefore, when his tag is set to 20000 + section, this ensures that when the section does not exceed 10000, both of them can be determined by tag without affecting each other)
Implement the button method just added
-(Void) btnOpenList :( UIButton *) sender {NSString * string = [NSString stringWithFormat: @ "% d", sender. tag-10000]; // the data stored in the selectedArr array corresponds to the header to facilitate future comparison if ([selectedArr containsObject: string]) {[selectedArr removeObject: string];} else {[selectedArr addObject: string];} [tableViewList reloadData];}
The above method is to click the group and then a new selectedArr array,
Next, we will implement reading selectedArr from the table and the new database to determine whether to expand the group.
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {NSString * indexStr = [NSString stringWithFormat: @ "% d", indexPath. section]; NSString * str = [titleArray objectAtIndex: indexPath. section]; NSArray * arr = [dataDic objectForKey: str]; static NSString * CellIdentifier = @ "UserCell"; UserTableViewCell * cell = [tableView metadata: CellIdentifier]; cell = nil; if (cell = nil) {cell = [[UserTableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier]; cell. selectionStyle = UITableViewCellSelectionStyleGray;} // you only need to add user information to the section expanded by the current group. if ([selectedArr containsObject: indexStr]) {NSDictionary * dic = [arr objectAtIndex: indexPath. row]; cell. headerphoto. image = [UIImage imageNamed: [dic valueForKey: @ "usericon"]; cell. nameLabel. text = [dic valueForKey: @ "name"]; cell. isOnLine. text = @ "[Online]"; cell. introductionLabel. text = @ "No dynamic"; cell. networkLabel. text = @ "4G";} return cell ;}
You can expand and close at this position, but the scissors on the left side have not been changed (the scissors are downward when being expanded, and the scissors are exclusive when being closed)
Now we need to adjust the Method
-(UIView *) tableView :( UITableView *) tableView viewForHeaderInSection :( NSInteger) section {UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, SCREEN_WIDTH, 30)]; view. backgroundColor = [UIColor whiteColor]; UILabel * titleLabel = [[UILabel alloc] initWithFrame: CGRectMake (30, 5, SCREEN_WIDTH, 30)]; titleLabel. text = [titleArray objectAtIndex: section]; [view addSubview: titleLabel]; UIImageView * imageVi Ew = [[UIImageView alloc] initWithFrame: CGRectMake (5, 5, 20, 20)]; imageView. tag = 20000 + section; // whether to expand the current settings image NSString * string = [NSString stringWithFormat: @ "% d", section]; if ([selectedArr containsObject: string]) {imageView. image = [UIImage imageNamed: @ "arrow_down.png"];} else {imageView. image = [UIImage imageNamed: @ "arrow_right.png"];} [view addSubview: imageView]; // Add a button to listen to the click group and close the group. UIButton * btn = [UIButton buttonWithType: UIButtonTypeCustom]; btn. frame = CGRectMake (0, 0, SCREEN_WIDTH, 50); btn. tag = 10000 + section; [btn addTarget: self action: @ selector (btnOpenList :) forControlEvents: UIControlEventTouchDown]; [view addSubview: btn]; return view ;}
The final result is as follows:
So far, it has basically completed the simple function of imitating the qq friends list.
The source code will be uploaded to the QQ Group Space
Apple Development Group: 414319235 welcome to join discussions