IOS开发。UITTableView

2016-05-06  本文已影响183人  李白不读书

UITableView是iOS系统中最重要的视图, 在常用的APP中随处可见。

UITableView

UITableView继承于UIScrollView,可以滚动。UITableView的每条数据对应的单元格叫做Cell,是UITableView的1个对象,继承于UIView。

UITableView可以分区显示, 每一个分区称为section,每一行称为row, 编号都从0开始。

系统提供了一个专门的类来整合section和row,叫做NSIndexPath。

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

[self.viewaddSubview:tableView];

[tableView release];

UITableView显示的相关属性

rowHeight     行高

separatarStyle   分隔线样式

separatarColor  分隔线颜色

tableHeaderView   UITableView的置顶视图

tableFooterView    UITableView置底视图

欲使用tableview先遵守协议
<UITableViewDataSource,UITableViewDelegate>
UITableView *tv = [[UITableView alloc]initWithFrame:CGRectZero type:0];

设置表视图的数据源代理
tv.dataSource = self;
PS:也可以单独设置数据源,那样的话只需要在单独设置的数据源里编写好3问等等。。。直接代理就可以
设置表视图的代理
tv.delegate = self;
这3步是必须的。
关于tableViewCell的一些相关设置方法。
如果是用XIB创建的话那么配合TableViewController就不需要以上三步

问1:一共有几个分区(这个可以不设置就默认1。)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{}

问2:每个分区多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}

问3:每行什么样,就是每一行要做什么
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}

答1:选中某行后,如何响应
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}
当 设置系统版的辅助视图格式
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton
只有点击圆圈i时才响应

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

答:每行多高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

某一行是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

某一行是哪种编辑状态(以下是删除状态)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return UITableViewCellEditingStyleDelete;
}
删除按钮的名称
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

当编辑操作被触发时,做什么(人性化一点  因为删除操作不可恢复,要弹出警告,创建 UIAlertController)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{}

删除Cell,第一个参数是数组。第二个参设置动画。  一般会先删除该行数据模型中的数据
 [self.XXX removeObjectAtIndex:indexPath.row];
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

设置分区头    尾的话就改foot   如果需要加图片或者其他属性就直接在tableview属性的 .tableHeaderView添加
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

//问1:改行是否可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

答1:移动后做什么
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
注册Cell,因为Cell带有XIB的话,则须使用Nib方式注册!

特别注意:如果用的是自定义Cell,那么必须用自定义的来开辟空间。

第一种注册方法:
    1.先尝试着去队列中取单元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    2.如果没有取到,再新建
    if (cell == nil) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
第二种:
                 PS: 单元格重用。如果单元格数量多 超出屏幕范围,那么用重用的话会提高效率
     为了让tableView知道自动创建的单元格什么样
    提前将单元格类注册一下
    【xxx class】返回该类型的描述信息

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell1"];
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell1"forIndexPath:indexPath];

Cell的属性:

accessoryType  用于设置风格:
Disclosure Indicator:带>    Detail Disclosure:带圆圈i和>  Checkmark:带钩  Detail:只带圆圈i。

动态获取Cell的高度

获取cell动态高度的步骤

  1. 在heightForRowAtIndexPath代理方法中这样设置,之后再去更改cell的frame即可。
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {

LFTableViewCell*cell = (LFTableViewCell*)[selftableView:tableViewcellForRowAtIndexPath:indexPath];

return cell.frame.size.height;

}

2.在cellForRowAtIndexPath中设置cell的frame. 其中self.originCellMaxY是模板cell中最底部控件的最大y值([tableViewdequeueReusableCellWithIdentifier:cellReuseID]获取的cell,不然cell重用的时候会出问题)

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

LFTableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:cellReuseID];

cell.titleLabel.text=self.allContent[indexPath.row];

// 获取不确定文字的高度

NSString*content =self.allContent[indexPath.row];

CGFloattestHeight = [selfsizeWithFont:[UIFontsystemFontOfSize:14.0]maxW:cell.frame.size.widthwithContent:content] +10;

// 重新设置cell的frame.

CGRectframe = cell.frame;

frame.size.height= testHeight +self.originCellMaxY;

cell.frame= frame;

return cell;

}
上一篇下一篇

猜你喜欢

热点阅读