国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

iOSUITableView動(dòng)態(tài)隱藏或顯示Item

2019-11-14 18:43:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

通過(guò)改變要隱藏的item的高度實(shí)現(xiàn)隱藏和顯示item

 

1.創(chuàng)建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, 具體的實(shí)現(xiàn)方法都已經(jīng)加了注釋

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    // 把要隱藏的item的高度設(shè)置為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 = @"點(diǎn)擊0行的時(shí)候隱藏第2行";    } else if(indexPath.row ==1) {        cell.textLabel.text = @"點(diǎn)擊1行的時(shí)候顯示第2行";    } else {        cell.textLabel.text = [NSString stringWithFormat:@"當(dāng)前行數(shù)%ld",indexPath.row];    }    return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    if (indexPath.row == 0) {        // 標(biāo)示是否被隱藏        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) {            // 隱藏的對(duì)應(yīng)item            [[self.tableView cellForRowAtIndexPath:tmpPath] setHidden:YES];            // 刷新被隱藏的item            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:tmpPath] withRowAnimation:UITableViewRowAnimationFade];        }];        NSLog(@"點(diǎn)擊了第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(@"點(diǎn)擊了第1行");    }}

 

3.效果

 

如果你不是在wb145230博客園看到本文,請(qǐng)點(diǎn)擊查看原文.


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新乡市| 滨海县| 平江县| 阿城市| 新邵县| 长沙市| 青冈县| 金川县| 凤山市| 邢台县| 商河县| 扎囊县| 宽甸| 永仁县| 抚宁县| 当雄县| 资兴市| 长寿区| 敖汉旗| 炎陵县| 错那县| 曲阳县| 增城市| 定兴县| 万年县| 美姑县| 陈巴尔虎旗| 墨竹工卡县| 贵定县| 论坛| 德昌县| 贺州市| 泉州市| 乐平市| 鄂托克前旗| 红安县| 广南县| 通渭县| 乐至县| 杭锦旗| 冀州市|