iOS常用开发技术总结回顾--UITableView
UITableView是app开发中常用到的控件,功能很强大,多用于数据的显示.不仅可以用于展示一般的表格,而且设置的属性资料等页面也常常用到UITableView,UITableView主要分为以下两种:
- 左边的是Plain风格:也就是系统默认的列表风格
- 右边的是Grouped风格:也就是常说的分块风格
在UITableView的每一行我们都称之为Cell,每一组称之为Section,Section又是由若干行组成的,每组又都包含Header和Footer,整个Table 又是有若干Section组成的,所以UITableView也有Header和Footer。下面总结了一些UITableView中常用方法的基本用法,在这里和大家一起分享,也方便自己以后的查阅回顾。
UITableView基本使用方法三部曲
1.实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource
2.UITableView对象的delegate设置为 self
3.实现这些代理方法
UITableView中包含的方法
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
这个方法返回 UITableview 有多少组。如果不实现该方法的话默认会返回1,即默认UITableView只有一组。
1 /** 2 * 返回组数 3 */ 4 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 5 { 6 return 2; 7 }
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
这个方法返回 每一组有多少个元素,即多少行。
1 /** 2 * 返回行数 3 */ 4 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 5 { 6 return 5; 7 }
(3)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法返回 每一行显示什么内容。注意cell的重用机制。
/** * 每一行显示什么内容 */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //定义一个cell的重用标识 static NSString * ID = @"ShowUserInfoCell"; //从缓存池中取出cell UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:ID]; //如果缓存池中没有 if (cell == nil) { // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:showUserInfoCellIdentifier]; } //设置背景颜色 cell.contentView.backgroundColor=[UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]; return cell; }
3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
这个方法返回指定的 row 的高度。
/** * 返回行高值 */ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath; { return 60; }
(4)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
这个方法返回指定组的header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
这个方法返回指定组的footer的高度,用法类似。
/** * 返回指定组的Header的高度 */ - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { //判断是否是第一组 if (section ==0) return 50.0f; else return 20.0f; }
(5)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
这个方法返回指定组header的title,如果该组header有返回view,那么title就不起作用了。
/** * 返回该组header的标题 */ - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (section == 0) { return @"First"; } else if (section == 1) { return @"Second"; } else { return nil; } }
(6) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
这个方法返回指定组headeView,如果没有,这个函数可以不返回view;如果有则会是title失效。
/** * 返回该组的headerView */ - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section == 0) { //通过Xib的方式加载头视图 UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView" owner: self options: nil] lastObject]; else { return nil; } }
(7) - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
这个方法是在用户选中某个行的cell的时候调用。但是前提是必须设置tableview的allowsSelection为YES。
- TableView.allowsSelection=YES;
- cell.selectionStyle=UITableViewCellSelectionStyleBlue; 设置cell被选中时的颜色
/** * cell的选中事件 */ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 1) { return; } else if(indexPath.section==0) { NSLog(@"被选中了"); } }
另外如何设置行之间的分割线
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
当设置为UITableViewCellSeparatorStyleNone 时就代表着不适用分割线。
(8) - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法返回当前选择的cell对象
(9) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
这个方法当用户点击cell右边的箭头(如果有的话)的时候调用
(12)- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法返回当前cell要执行是哪种编辑模式。
实现此方法首先要设置tableView进入编辑模式:
- [TableView setEditing:YES animated:YES];
如果要退出编辑模式,肯定就是设置为NO
/** * 返回当前cell执行的编辑模式是什么 */ - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; }
-(void) tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath;
这个方法对应上面的代码,告知用户当前哪个cell执行了上面的编辑模式
1 /** 2 * 执行cell当前的编辑操作 3 */ 4 -(void) tableView:(UITableView *)aTableView commitEditingStyle(UITableViewCellEditingStyle) editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath 5 { 6 [myArray removeObjectAtIndex:indexPath.row]; 7 [myTableView reloadData]; 8 }

浙公网安备 33010602011771号