IOS UITableView dynamically hides or displays Item and iosuitableview

Source: Internet
Author: User

IOS UITableView dynamically hides or displays Item and iosuitableview

Hide and display items by changing the height of items to be hidden

 

1. Create UITableView

#import "ViewController.h"@interface ViewController ()@property(nonatomic, strong)UITableView *tableView;@property(nonatomic, assign)BOOL isHiddenItem;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.isHiddenItem = NO;    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];    self.tableView.delegate = self;    self.tableView.dataSource = self;    [self.view addSubview:self.tableView];}

 

2. UITableView delegate. The specific implementation methods have been annotated.

-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {// set the height of the item to be hidden to 0 if (indexPath. row = 2 & self. isHiddenItem) {return 0;} return 100;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return 5;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @ "cell"]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @ "cell"];} if (indexPath. row = 0) {cell. textLabel. text = @ "Hide 2nd rows when you click 0";} else if (indexPath. row = 1) {cell. textLabel. text = @ "2nd rows are displayed when one row is clicked";} else {cell. textLabel. text = [NSString stringWithFormat: @ "current row % ld", indexPath. row];} return cell;}-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {if (indexPath. row = 0) {// indicates whether self is hidden. isHiddenItem = YES; // obtain the NSIndexPath * tmpPath = [NSIndexPath indexPathForRow: indexPath. row + 2 inSection: indexPath. section]; [UIView animateWithDuration: 0.3 animations: ^ {[self. tableView cellForRowAtIndexPath: tmpPath]. alpha = 0.0f;} completion: ^ (BOOL finished) {// hide the corresponding item [[self. tableView cellForRowAtIndexPath: tmpPath] setHidden: YES]; // refresh the hidden item [self. tableView reloadRowsAtIndexPaths: [NSArray arrayWithObject: tmpPath] withRowAnimation: UITableViewRowAnimationFade];}]; NSLog (@ "0th rows clicked");} else if (indexPath. row = 1) {self. isHiddenItem = NO; NSIndexPath * tmpPath = [NSIndexPath indexPathForRow: indexPath. row + 2 inSection: indexPath. section]; [UIView animateWithDuration: 0.3 animations: ^ {[self. tableView cellForRowAtIndexPath: tmpPath]. alpha = 1.0f;} completion: ^ (BOOL finished) {[self. tableView cellForRowAtIndexPath: tmpPath] setHidden: YES]; [self. tableView reloadRowsAtIndexPaths: [NSArray arrayWithObject: tmpPath] withRowAnimation: UITableViewRowAnimationFade];}]; NSLog (@ "1st rows clicked ");}}

 

3. Results

 

If you did not see this article in wb145230, click to view the original article.

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.