码迷,mamicode.com
首页 > 移动开发 > 详细

ios UITableView 相关

时间:2014-05-09 21:03:32      阅读:424      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   ext   c   

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;
}

tableView的刷新:

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

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

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

左滑动会调用 commitEditingStyle 方法 commitEditingStyle 中需要判断是 添加还是删除

#pragma mark - tableView的代理方法
/**
 *  如果实现了这个方法,就自动实现了滑动删除的功能
 *  点击了删除按钮就会调用
 *  提交了一个编辑操作就会调用(操作:删除\添加)
 *  @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;
}




ios UITableView 相关,布布扣,bubuko.com

ios UITableView 相关

标签:style   blog   class   code   ext   c   

原文地址:http://blog.csdn.net/codywangziham01/article/details/25412271

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!