表视图:
表视图继承自UIScrollView,所以可以滚动表视图的每一条数据都是显示在UITableViewCell对象中
表视图可以分区显示数据,每个分区称为一个section,每一行称为row,编号都是从0开始
重要的属性:
style样式: plain 和group
分割线样式:separatorStyle
分割线颜色:separatorColor
行高:rowHeight
两个必须实现的方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
表视图的重用机制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%s",__func__);
static NSString *cellIdentify = @"cellIdentify";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
//从cellIdentify命名的重用池中取得cell
if (!cell) {
//如果不存在 创建一个cell 并且将他放到以cellIdentify命名的重用池里面 方便下次使用
cell = [[[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify]autorelease];
}
NSLog(@"section === %d row === %d",indexPath.section,indexPath.row);
NSString *str = [_tableArray objectAtIndex:indexPath.row];
[cell.textLabel setText:str];
return cell ;
}
指定哪一行可以移动
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
指定tableView编辑的样式 (添加,删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowIndexPath:(NSIndexPath *)indexPath;
关于表视图的移动:
首先让tableView处于编辑状态,方法如上;
指定哪些tableView可以移动:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
完成移动
- (void)tableView :(UITableView *)tableView moveRowAtIndexPath :(NSIndexPath *)destinationIndexPath;
监测移动过程 实现限制跨区移动
UITableViewController
继承UITableView 自带一个tableView 根视图就是tableView
模板自带编辑移动的相关代码