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

UI学习 第六章 UITableView

2015-11-24 20:20 405 查看
UI学习 第五章 UITableView

tableView下空cell的消除方法:table.tableFooterView = [[UIView alloc]init];

1.创建

UITableView*tableView
= [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];

2.行高

tableView.rowHeight=
80;

3.分割线样式

tableView.separatorStyle=
UITableViewCellSeparatorStyleSingleLine;

4.分割线颜色

tableView.separatorColor=
[UIColorwhiteColor];

5.给TableView添加背景图,可以不设置背景图的frame

UIImageView*bgView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"1"]];

tableView.backgroundView=
bgView;

6.tableView顶(底)部视图,其frame只有高有效

UIView*header = [[UIViewalloc]initWithFrame:CGRectMake(10,10,0,20)];

header.backgroundColor= [UIColorgrayColor];

tableView.tableHeaderView = header;

代理方法(
tableView.dataSource=
self;
tableView.delegate=self;

)

0.设置每行行高

-(CGFloat)tableView:(UITableView*)tableView
heightForRowAtIndexPath:(NSIndexPath*)indexPath{

return
90;

}

tableView.dataSource=
self;

tableView.delegate=self;

1.table的组数默认是一组

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

return
1;

}

2.设置每组的行数

-(NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section{

if (section ==
0) {

return
1;

}elseif
(section ==
1){

return
10;

}

return
4;

}

3.设置每组的cell

-(UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath{//indexPath包含了第几组:indexPath.section和第几行:indexPath.row

static
NSString *identifier =
@"Cell";//重用机制标识

UITableViewCell*cell
= [tableView
dequeueReusableCellWithIdentifier:identifier];//根据重用标识,到重用池找对应的cell

if (cell ==
nil) {

cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];//创建一个cell,设置其样式以及其标识

}

// cell.backgroundColor = [UIColor clearColor];

cell.textLabel.text=
[NSStringstringWithFormat:@"第%zi行,第%zi组",indexPath.row,indexPath.section];//设置cell的文本信息

cell.imageView.image=
[UIImageimageNamed:@"58"];

return cell;//将设置好的cell返回

}

cell样式

子标题式cell

cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:iden];

子标题在最右边式cell

[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue1reuseIdentifier:iden];

子标题紧挨标题右边式cell

[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleValue2reuseIdentifier:iden];

提示用户可点,点击后调到下级界面

cell.accessoryType=
UITableViewCellAccessoryDisclosureIndicator;

提示用户可点,点击按钮(叹号)会有相关按钮弹出,点击cell会跳转到下级界面

cell.accessoryType=
UITableViewCellAccessoryDetailDisclosureButton;

显示对勾

cell.accessoryType=
UITableViewCellAccessoryCheckmark;

提示用户可点击,点击后会有相关提示弹出

cell.accessoryType=
UITableViewCellAccessoryDetailButton;

4. 每组顶部视图的高度

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

return20;

}

5. 自定义每组头视图

-(UIView*)tableView:(UITableView*)tableView
viewForHeaderInSection:(NSInteger)section{

//自定义每组头视图

UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,200,40)];

view.backgroundColor= [UIColorbrownColor];

return view;

}

6.自定义尾部视图

-(UIView*)tableView:(UITableView*)tableView
viewForFooterInSection:(NSInteger)section{

UIView*view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,200,40)];

view.backgroundColor= [UIColorpurpleColor];

return view;

}

7.设置每行的高度

-(CGFloat)tableView:(UITableView*)tableView
heightForRowAtIndexPath:(NSIndexPath*)indexPath{

if
(indexPath.row==
0) {

return
80;

}

return
40;

}

8.中间视图显示东西

-(NSString*)tableView:(UITableView*)tableView
titleForHeaderInSection:(NSInteger)section{

return [NSStringstringWithFormat:@"第%zi组",section];

}

9.点击Button,cell的个数加一

-(void)addCell{

NSLog(@"=====");

[_arrayaddObject:@"123"];

[_tableViewreloadData];//当tableView的数据源发生改变时,调用该方法,会更新tableView的显示,

}

cell选中的背景色

cell.selectionStyle=
UITableViewCellSelectionStyleDefault;

自定义cell选中背景

view.backgroundColor=
[UIColorblueColor];

cell.selectedBackgroundView = view;

选中状态下字体颜色

cell.textLabel.highlightedTextColor=
[UIColorwhiteColor];

自定义右侧点击视图

cell.accessoryView

10.当cell的accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法

-(void)tableView:(UITableView*)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath{

}

11.取消选中时调用

-(void)tableView:(UITableView*)tableView
didDeselectRowAtIndexPath:(NSIndexPath*)indexPath{

NSLog(@"----%zi",indexPath.row);

}

12.cell被选中时调用

-(void)tableView:(UITableView*)tableView
didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

NSLog(@"----%zi",indexPath.row);

// [tableView deselectRowAtIndexPath:indexPath animated:YES];//当cell被点击时,取消选中状态

}

13.是否允许编辑

-(BOOL)tableView:(UITableView*)tableView
canEditRowAtIndexPath:(NSIndexPath*)indexPath{

return
YES;

}

14.提交修改动作:在执行删除之前,需要先移除数组中的元素

-(void)tableView:(UITableView*)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

[_arrayremoveObjectAtIndex:indexPath.row];

[tableView
deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];//删除行

}

cell左侧小图标添加方法

cell.imageView.image=
[UIImageimageNamed:@"58"];

重用机制

1、创建cell时,不从重用池找,进来就创建

NSString *identifier = [NSString stringWithFormat:@"cell"]; // 设置cell 标识

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier]; // 创建cell的同时为其附上设置的标识,进来就创建带有相同标识的cell

这种方法很耗内存,为了解决它。引用重用池,我们把之前创建好的cell放进重用池内,下面在用,直接从重用池中拿出来。拿出来后,此cell便不在重用池内。也就是说内容为“123”的cell被拿出来后,下一个显示的cell是上面又一次滑进去的空的cell。然而并不是上面每滑进去一个,下面就能拿出来用。而是当下面要‘ 创建’的这个cell 的标示符 与 上面被滑进去的cell中的某一cell的标识符一致时(按标识找),重用池中的这个cell才会被拉出去用。否则就生产一个带这个标识的cell。

2. 为了解决被找到拿出来用的cell有原内容

a、拿出来用之前,把原内容清空 cell.textLabel.text = nil;

b、给创建的每一个cell赋不同的标识

NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识。进入此方法,下一个要显示的cell就被赋了一个唯一标识,比如cell0组15行,与上面滑进去cell的标识都不同。它会被重新创建

c.、进入重用池找的时候,不用标示符

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];//处理重用bug很不友好的方式,不建议使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: