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.