TableView 是object-c最最最常用的控件了 、来掌握它吧
一)UITableView所在的UIViewController声明两个delegate:UITableViewDelegate和UITableViewDataSource。
二)将UITableView对象的delegate设置成self。
三)根据实际需要实现delegate的具体方法,这里简要介绍一下常用的方法和属性。
1、返回tableview有多少个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2、返回对应的section有多少个元素,也就是每个section对应有多少个cell
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
3、 返回指定的row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
4、返回指定的section的header view的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
5、返回指定的section的footer view的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
6、 返回指定row的cell,在此函数中用户可以根据自己的需求定义cell的属性和显示风格等(主标题cell.textLabel,副标题cell.detailTextLabel,背景cell.imageView,图标cell.accessoryType等等)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
7、 返回指定section的header的titile。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
8、 返回指定section header的view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
9、 用户选中某cell时的回调函数
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
10、 获取某一cell对象
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
如果想让cell能响应选中事件,但是选中后的颜色不发生改变的话,设置cell.selectionStyle = UITableViewCellSelectionStyleNone。
如果想删除cell之间的分割线,设置 tableview.separatorStyle = UITableViewCellSeparatorStyleNone。