iOS UITableView動態隱藏或顯示Item,iosuitableview

來源:互聯網
上載者:User

iOS UITableView動態隱藏或顯示Item,iosuitableview

通過改變要隱藏的item的高度實現隱藏和顯示item

 

1.建立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, 具體的實現方法都已經加了注釋

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    // 把要隱藏的item的高度設定為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 = @"點擊0行的時候隱藏第2行";    } else if(indexPath.row ==1) {        cell.textLabel.text = @"點擊1行的時候顯示第2行";    } else {        cell.textLabel.text = [NSString stringWithFormat:@"當前行數%ld",indexPath.row];    }    return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.row == 0) {        // 標示是否被隱藏        self.isHiddenItem = YES;                // 擷取要隱藏item的位置        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) {            // 隱藏的對應item            [[self.tableView cellForRowAtIndexPath:tmpPath] setHidden:YES];            // 重新整理被隱藏的item            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tmpPath] withRowAnimation:UITableViewRowAnimationFade];        }];        NSLog(@"點擊了第0行");    } 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(@"點擊了第1行");    }}

 

3.效果

 

如果你不是在wb145230部落格園看到本文,請點擊查看原文.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.