ios UITableView 相关

1、tableView 实现的方法 无分组的cell

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contacts.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建cell
    MJContactCell *cell = [MJContactCell cellWithTableView:tableView];
   
    // 2.设置cell的数据
    cell.contact = self.contacts[indexPath.row];
    
    return cell;
}

2、tableView的刷新:

* 局部刷新(使用前提: 刷新前后, 模型数据的个数不变)

- (void)reloadRows:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

3、* 局部删除(使用前提: 模型数据降低的个数 == indexPaths的长度)
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

4、tableview 在storyboard 创建的时候 假设tableview中的cell是写死的,当自己定义controller关联的时候 要记得删掉数据源中的方法, numberOfRowsInSection 方法等


左滑动会调用 commitEditingStyle 方法 commitEditingStyle 中须要推断是 加入还是删除

#pragma mark - tableView的代理方法
/**
 *  假设实现了这种方法,就自己主动实现了滑动删除的功能
 *  点击了删除button就会调用
 *  提交了一个编辑操作就会调用(操作:删除\加入)
 *  @param editingStyle 编辑的行为
 *  @param indexPath    操作的行号
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) { // 提交的是删除操作
        // 1.删除模型数据
        [self.contacts removeObjectAtIndex:indexPath.row];
        
        // 2.刷新表格
        // 局部刷新某些行(使用前提:模型数据的行数不变)
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        
        // 3.归档
        [NSKeyedArchiver archiveRootObject:self.contacts toFile:MJContactsFilepath];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // 1.改动模型数据
        MJContact *contact = [[MJContact alloc] init];
        contact.name = @"jack";
        contact.phone = @"10086";
        [self.contacts insertObject:contact atIndex:indexPath.row + 1];
        
        // 2.刷新表格
        NSIndexPath *nextPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[nextPath] withRowAnimation:UITableViewRowAnimationBottom];
//        [self.tableView reloadData];
    }
}

// 让tableView进入编辑状态

[self.tableView setEditing:!self.tableView.isEditing animated:YES];
当实现editStyleForRowAtIndexPath 的是时候,当点击编辑的时候就会调用此方法,此方法是询问编辑的状态的
/**
 *  当tableView进入编辑状态的时候会调用,询问每一行进行如何的操作(加入\删除)
 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row %2 ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleInsert;
}




posted @ 2015-01-29 21:11  zfyouxi  阅读(119)  评论(0编辑  收藏  举报