1.设置每个cell之间的分割线的一些属性,设置每个分割线的颜色
_tableView.separatorColor = [UIColor redColor];
2.隐藏分割线
_tableView.separatorStyle = UITableViewRowAnimationNone;
3.设置分割的位置
if([_tableView respondsToSelector:@selector(setSeparatorInset:)])
{中间那个数是距离左边的距离,依次调节
_tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 50);
}
4.刷新
[_tableView reloadData];
5.系统自带的一个编辑按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
6.响应编辑按钮
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
继承父类
[super setEditing:editing animated:animated];
[_tableView setEditing:editing animated:animated];
7.初始化等
初始化并且设置tableView的frame
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
8.设置代理
_tableView.delegate = self;
_tableView.dataSource =self;
}
返回分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
9.必须要实现的两个方法,每个section对应的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
;
10.返回section分区所对应的标题,头视图
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
11.设置对应的角标,在分区中可以看到
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
12.返回头视图,这里返回的透视图是一个vie可以在里面设置点击事件
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *vie = [[UIView alloc]init];
vie.backgroundColor = [UIColor redColor];
return vie;
}
13.设置头视图的高度,同理也可以设置脚视图的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
14.indexPath:包含两个值:
row:行
section:分区
这个方法的返回值,返回的是indexPath所对应的那个cell
创建出来的cell的个数,是满屏的cell的数量+1
(满屏数量=tableView的高度/cell的高度 + 1)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid = @"cell";
当出现一个cell时,首先要从tableView的可充用队列中进行取闲置的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
如果没有闲置的cell这时需要创建
UITableViewCellStyleSubtitle :创建一个带有副标题的cell
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
15.设置标题的内容
cell.textLabel.text = _arry[indexPath.section][indexPath.row];
16.设置cell右边的按钮
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
17.点击右边按钮对应的数
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
18.返回编辑的方式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
19.响应对应的方式
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
删除,首先从数组删除,然后从tableview删除
[_arry removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
添加也类似以上内容
[_arry insertObject:@"pp" atIndex:indexPath.row];
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
20.左滑出来按钮
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
;
21.对应的ios8里面的新特性
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"pp个头" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
}];
//这里设置背景颜色
action.backgroundColor = [UIColor blueColor];
UITableViewRowAction *actionP = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"小王八" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
}];
return @[action,actionP];
}
22.设置每一个cell的高度
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;
23.点击某一个cell时调用该方法
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;