UITableView的编辑模式

1.UITableView的大部分操作都是在代理的方法中进行的。

2.在设置UITableView的多选模式时,要先定义一个待操作数组,用于存储已被选中等待操作的Cell。

NSMutableArray *_waitArray;

3.定义一个BOOL型变量 记录是否处于编辑状态(关闭导航自动下沉 self.automaticallyAdjustsScrollViewInsets = NO;)

BOOL _isEdit;

4.设置cell选中的风格样式
        cell.selectionStyle = UITableViewCellSelectionStyleGray;

5.设置cell右边的附属按钮样式
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator

6.设置cell的主标题
    cell.textLabel.text = str;
    //设置cell的副标题
    cell.detailTextLabel.text = @"副标题";

7.分区数量,默认1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

8.每个分区里的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

9.创建Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
10.分区的头标题和脚标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
11.行高(默认44),分区头标题高度,脚标题高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
12.将一个view设成头标题
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
13.选中表格视图某一行后,触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
14.行被选中后,自动变回反选状态的方法
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
15.- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    //必须有蓝色的按钮出现,才能点击执行这个方法,点出附属按钮
    NSLog(@“点击了第%d分区%d行",indexPath.section,indexPath.row);
}
16.- (void)customEditMethod
{//自定义按钮实现编辑方法时,需要设置一个bool型成员变量
    _isEditing = !_isEditing;
    [_tableView setEditing:_isEditing animated:YES];
}
17.作用 系统自带的编辑按钮时必须按照下面的方式重写编辑函数,固定写法
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [myTableView setEditing:editing animated:animated];
}
18.- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{//编辑的方法,如果其它的编辑都不设,只设了这一个,那么就会有cell右滑出现删除的按钮,也是比较常用的
    if (editingStyle == UITableViewCellEditingStyleDelete){
        //删除
        [[dataArr objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];  //删除数据源记录
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];//删除cell
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        //插入
        [[dataArr objectAtIndex:indexPath.section] insertObject:@"a new cell" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
19.- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{//删除按钮的字
    return @"删";
}
20.- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{//返回编辑模式,默认是删除
    if (indexPath.section) {
        return UITableViewCellEditingStyleInsert;
    }
    return UITableViewCellEditingStyleDelete;
}
21.- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{//移动,不实现这个代理方法,就不会出现移动的按钮
    NSString *text=[[[dataArr objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row] copy];
    [[dataArr objectAtIndex:sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
    [[dataArr objectAtIndex:destinationIndexPath.section] insertObject:text atIndex:destinationIndexPath.row];
    [text release];
}
22.- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{//默认所有的都是可以编辑的(增,删)
    return YES;
}
23.- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{//默认所有的都是可以移动的
    return YES;
}












posted @ 2015-04-02 19:36  小学生之码  阅读(288)  评论(0编辑  收藏  举报