您的位置:首页 > 产品设计 > UI/UE

UITableView总结:基本使用方法介绍

2011-12-19 17:36 751 查看
最近在做iphone GRE背单词过程中涉及到很多UITableView的操作,借此机会好好梳理总结一下。本节主要介绍UITableView的基本使用方法。

一)UITableView所在的UIViewController声明两个delegate:UITableViewDelegate和UITableViewDataSource。

二)将UITableView对象的delegate设置成self。

三)根据实际需要实现delegate的具体方法,这里简要介绍一下常用的方法和属性。

(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 该方法返回tableview有多少个section。

(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 该方法返回对应的section有多少个元素,也就是每个section对应有多少个cell。

(3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 该方法返回指定的row高度。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 该方法返回指定的section的header view的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 该方法返回指定的section的footer view的高度。

(4)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 该方法返回指定row的cell,在此函数中用户可以根据自己的需求定义cell的属性和显示风格等(主标题cell.textLabel,副标题cell.detailTextLabel,背景cell.imageView,图标cell.accessoryType等等)。

(5)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 该函数返回指定section的header的titile。

(6)- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 该函数返回指定section header的view

(7) -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 用户选中某cell时的回调函数。

(8)- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath 该方法获取某一cell对象。

(9)如果想让cell能响应选中事件,但是选中后的颜色不发生改变的话,设置cell.selectionStyle = UITableViewCellSelectionStyleNone。

(10)如果想删除cell之间的分割线,设置

tableview.separatorStyle = UITableViewCellSeparatorStyleNone。

以上介绍的方法及属性仅仅是GRE用到的,另外还有很多方法和属性未涉及。本篇文章只是抛砖引玉,等我们需要用到的时候可以参考xCode的联机帮助手册。

下节我给大家介绍一下UITableViewCell定制的用法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: