IOS development UI-hierarchical display of tableView, iostableview

Source: Internet
Author: User

IOS development UI-hierarchical display of tableView, iostableview

The cell-level display of a tableView varies with the search ideas on the Internet. The following describes my Implementation ideas.

Store the cell status according to the root title and add it to the dictionary.

Let's just talk about the code.

 

 

# Define SCREEN_HEIGHT [UIScreen mainScreen]. bounds. size. height # define SCREEN_WIDTH [UIScreen mainScreen]. bounds. size. width @ interface CellTreesController () <UITableViewDelegate, UITableViewDataSource> @ property (nonatomic, strong) UITableView * MyTableView; @ property (nonatomic, strong) NSMutableArray * dataArray; // store the title @ property (nonatomic, strong) NSMutableDictionary * boolDcitionary; // store whether the corresponding partition section is expanded @ end @ implementation CellTreesController-(void) viewDidLoad {[super viewDidLoad]; _ MyTableView = [[UITableView alloc] initWithFrame: CGRectMake (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style: UITableViewStylePlain]; _ MyTableView. dataSource = self; _ MyTableView. delegate = self; _ MyTableView. tableFooterView = [UIView new]; [self. view addSubview: _ MyTableView]; // group header data for (int I = 0; I <20; I ++) {NSString * titleString = [NSString stringWithFormat: @ "group % d", I + 1]; [self. dataArray addObject: titleString];} // Do any additional setup after loading the view .} -(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} # pragma mark-dalegate dataSource // area header view height-(CGFloat) tableView :( UITableView *) tableView heightForHeaderInSection :( NSInteger) section {return 44 ;} // area header view-(UIView *) tableView :( UITableView *) tableView viewForHeaderInSection :( NSInteger) section {UIView * headerView = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 0, SCREEN_WIDTH, 44)]; UILabel * lable = [[UILabel alloc] initWithFrame: CGRectMake (20, 7,150, 30)]; lable. text = self. dataArray [section]; lable. textColor = [UIColor orangeColor]; headerView. tag = 1000 + section; // Add gesture-> control cell expansion [headerView addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (clickView :)]; [headerView addSubview: lable]; return headerView;} // section-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 20;} // row-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {NSString * clickTag = [NSString stringWithFormat: @ "% ld", section + 1000]; if ([self. boolDcitionary [clickTag] integerValue] = 1) {return 6 ;}else {return 0 ;}// cell-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * identifier = @ "myTableViewCell"; UITableViewCell * cell = [tableView metadata: identifier]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier];} cell. textLabel. text = [NSString stringWithFormat: @ "row % ld", indexPath. row + 1]; return cell;} # pragma mark-tap gesture Click Event-(void) clickView :( UITapGestureRecognizer *) action {NSString * clickTag = [NSString stringWithFormat: @ "% ld", action. view. tag];

// When the second cell is expanded, the expansion status of the previous cell remains unchanged. // if ([self. boolDcitionary [clickTag] integerValue] = 0) {[self. boolDcitionary setValue: @ (1) forKey: clickTag];
} Else {// If the status is 1, expand [self. boolDcitionary setValue: @ (0) forKey: clickTag];}


/// The expanded cell is automatically disabled when the second cell is expanded.
/// If the status is 0, it indicates that the service is disabled.
// If ([self. boolDcitionary [clickTag] integerValue] = 0 ){
//
// For (int I = 1000; I <(1000 + 20); I ++ ){
// NSString * clickStr = [NSString stringWithFormat: @ "% d", I];
// If ([clickStr isw.tostring: clickTag]) {
//
// [Self. boolDcitionary setValue: @ (1) forKey: clickStr];
//
//} Else {
//
// [Self. boolDcitionary setValue: @ (0) forKey: clickStr];
//}
//
//}
//} Else {
//// If the status is 1, it indicates expansion.
// [Self. boolDcitionary setValue: @ (0) forKey: clickTag];
//}



// Refresh tableView (indispensable) [_ MyTableView reloadData];} # pragma mark-lazy loading-(NSMutableDictionary *) boolDcitionary {if (! _ BoolDcitionary) {_ boolDcitionary = [NSMutableDictionary dictionary];} return _ boolDcitionary;}-(NSMutableArray *) dataArray {if (! _ DataArray) {_ dataArray = [NSMutableArray array];} return _ dataArray ;}

 

Someone asked me again, although my cell was expanded, it was all straight up and down, and there was no animation effect yet. Next I will introduce you to the implementation of the animation effect of cell expansion.

Implement the following UITableViewDelegate Method

-(Void) tableView :( UITableView *) tableView willDisplayCell :( UITableViewCell *) cell forRowAtIndexPath :( NSIndexPath *) indexPath;

Write a simple animation effect of UITableViewCell.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{        CATransform3D rotation;    rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);    rotation.m34 = 1.0/ -600;        cell.layer.shadowColor = [[UIColor blackColor]CGColor];    cell.layer.shadowOffset = CGSizeMake(10, 10);    cell.alpha = 0;    cell.layer.transform = rotation;        [UIView beginAnimations:@"rotation" context:NULL];    [UIView setAnimationDuration:0.8];    cell.layer.transform = CATransform3DIdentity;    cell.alpha = 1;    cell.layer.shadowOffset = CGSizeMake(0, 0);    [UIView commitAnimations];}

 

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.