UITableView

作者: 流水点点 | 来源:发表于2016-04-10 13:14 被阅读109次

介绍:UITabelView是iOS开发中最常用, 也是最灵活的控件.用途最广泛.

两种风格:

UITableViewStylePlain 和 UITableViewStyleGrouped。

UITableView中只有行的概念,每一行就是一个UITableViewCell。UITableViewCell内置好的控件contentView父控件

contentView = (textLabel,detailTextLabel)+ UIImage控件(imageView)分别用于容器、显示内容、详情和图片。

代理方法、数据源方法:<UITableviewDelegate,UITableviewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView )tableView      numberOfRowsInSection:(NSInteger)section{        return 5;}

//有多少组(默认为1)

- (UITableViewCell )tableView cellForRowAtIndexPath:(  NSIndexPath identifily = @"cellIdentifily";      UITableViewCell )tableView willDisplayCell:(UITableViewCell )indexPath{          NSLog(@"willDisplayCell");} //cell内容设置,属性设置

- (void)tableView:(UITableView )cell forRowAtIndexPath:(NSIndexPath)tableView    didEndDisplayingHeaderView:(UIView )tableView    didEndDisplayingFooterView:(UIView )tableView    heightForRowAtIndexPath:(NSIndexPath )tableView    heightForHeaderInSection:(NSInteger)section{              return 15.0f;}  //滑动时,cell消失时调用

点击cell时调用

- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath;

离开点击时调用

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

重用机制->滚筒原理->只有固定->屏的视图超出后重用这一屏的视图一直滚动循环使用只需更改上面的数据  DataSource有两个必须实现的代理方法没有实现就会出现警告——>运行崩溃

Required:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;//行数

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;//cell展示内容

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;设置表头的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;//设置表尾的高度

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;//设置表头的视图

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;//设置表尾的视图

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;//设置表头的标题

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;//设置表尾的标题

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{return @"首页";}//设置表头的标题

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{return100;}//设置表尾的高度

//每个分组上边预留的空白高度

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section

{return20;}

刷新数据

刷新整个TableVIew[self.tableView reloadData];

1.刷新一个cell、

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];[self.tableViewreloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationFade];

2.刷新一个section

NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];[self.tableView reloadSections:indexSetwithRowAnimation:UITableViewRowAnimationFade];

转载:

iOS tableview那些事

相关文章

网友评论

    本文标题:UITableView

    本文链接:https://www.haomeiwen.com/subject/jxbblttx.html