TableView的动态更新操作(无需重新加载数据源)

项目中我们经常会用到TableView展示一个Cell,Cell的数据来源于我们自定义的一个Model类,那么对于TableView我们有以下几种场景。

1. 添加操作: 在该列表页面顶部有一个按钮叫做新建,点击后进入一个新的添加页面,添加完成之后,返回到列表页更新数据。

2. 更新操作:点击列表中cell进入编辑页面,编辑页面其实就是这个Model类中属性的一个展示,对其中某些属性进行更改后,返回到列表页更新数据。

3. 删除操作:点击列表中cell进入编辑页面,该页面有个删除按钮,点击删除按钮,操作成功后,返回列表页面更新数据。

对于以上场景,一般比较笨的方法是回到列表页面后重新从服务器加载一遍数据,但太耗费资源 pass。

另一种办法就是对数据源(数组 datasource)进行更新:

  • 添加操作执行后,在数据源 datasource中insertObject:obj 然后reload列表。
  • 更新操作后,在数据源datasource找到该item进行更新然后reload列表。
  • 删除操作后,在数据源datasource找到该item进行删除操作然后reload列表。

要达到这种效果,我们需要将数据源datasource传入下一个页面或者通过消息通知机制通知datasource(当操作执行时),如果项目中该功能列表非常多的时候,每次都进行重复的操作着实有些麻烦,所以在这里,我封装了一个tableview的category用于解决以上的问题,工作原理如下:

1. 给tableview增加注册监听消息(添加,更新,删除,刷新)

- (void)addDataChangedObserver:(NSMutableArray *)datasource
                    primaryKey:(NSString *)primaryKey
                   changeBlock:(DataChangeBlock)changeBlock {
    
    //给类别关联数据源属性,存储tableview数据源
    objc_setAssociatedObject(self, (__bridge const void *)(kDatasource), datasource, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    //记录数据源中model的唯一标识
    objc_setAssociatedObject(self, (__bridge const void *)(kPrimaryKey), primaryKey, OBJC_ASSOCIATION_COPY_NONATOMIC);
    //记录回调的block
    objc_setAssociatedObject(self, (__bridge const void *)(kChangeBlock), changeBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    //添加监听方法
    [self removeObserver];
    [self addObserver];
    
}
View Code
#pragma mark - Observer Operation

- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(add:) name:TableViewOperationAddNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(delete:) name:TableViewOperationDeleteNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(update:) name:TableViewOperationUpdateNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refresh:) name:TableViewOperationRefreshNotification object:nil];
}
View Code

2. 收到消息通知后调用的方法

#pragma mark - Actions

- (void)add:(NSNotification *)notification {
    NSLog(@"调用了tableview的add消息事件");
    id obj = notification.object;
    //获取数据源数据进行对象添加
    NSMutableArray *datasource = [self getPropertyKey:kDatasource];
    if (datasource && obj) {
        if (datasource.count > 0) {
            if ([obj isKindOfClass:[datasource[0] class]]) {
                [datasource insertObject:obj atIndex:0];
                [self reloadData];
            }
        }
    }
    
    [self callback:TableViewOperationAdd obj:obj];
}

- (void)delete:(NSNotification *)notification {
    NSLog(@"调用了tableview的delete消息事件");
    id objNotify = notification.object;
    
    //从数据源中删除一个对象并刷新tableview
    [self changeDataSourceWithObj:objNotify operationType:TableViewOperationDelete];
    [self callback:TableViewOperationDelete obj:objNotify];
}

- (void)update:(NSNotification *)notification {
    NSLog(@"调用了tableview的update消息事件");
    id objNotify = notification.object;
    
    //从数据源更新一个对象并刷新tableview
    [self changeDataSourceWithObj:objNotify operationType:TableViewOperationUpdate];
    [self callback:TableViewOperationUpdate obj:objNotify];
}

- (void)refresh:(NSNotification *)notification {
    NSLog(@"调用了tableview的refresh消息事件");
    id obj = notification.object;
    
    //刷新tableview
    [self reloadData];
    [self callback:TableViewOperationRefresh obj:obj];
}

- (void)callback:(TableViewOperationType)operationType obj:(id)obj {

    DataChangeBlock block = objc_getAssociatedObject(self, (__bridge const void*)kChangeBlock);
    NSIndexPath *indexPath = objc_getAssociatedObject(self, (__bridge const void*)kIndexPath);
    if (block) {
        block(operationType, indexPath, obj);
    }
}
View Code
- (void)changeDataSourceWithObj:(id)objNotify operationType:(TableViewOperationType)operationType {
    
    //取出数据源
    NSMutableArray *datasource = [self getPropertyKey:kDatasource];
    
    //取出对象主键字段名
    NSString *primaryKey = [self getPropertyKey:kPrimaryKey];
    
    //取出对象主键字段对应的value值
    NSString *valueNotify = [self getObjPropertyValueByKey:primaryKey obj:objNotify];
    if (objNotify) {
        [datasource enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            NSString *value = [self getObjPropertyValueByKey:primaryKey obj:obj];
            if ([valueNotify isEqualToString:value]) {
                if (operationType == TableViewOperationDelete) {
                    [datasource removeObject:objNotify];
                    NSLog(@"对象删除成功,刷新数据");
                } else if (operationType == TableViewOperationUpdate) {
                    [datasource replaceObjectAtIndex:idx withObject:objNotify];
                    NSLog(@"对象更新成功,刷新数据");
                }
                [self reloadData];
                *stop = YES;
            }
        }];
    }
}
View Code

主要用到了runtime的相关知识,动态的为tableview关联了属性,当程序运行到执行相应的操作时,根据关联的属性来操作数据源,达到效果。

如何使用??

1. 非常简单,在需要的viewcontroller中添加如下方法:

[self.tableview addDataChangedObserver:self.datasource primaryKey:@"pid" changeBlock:^(TableViewOperationType operationType, NSIndexPath *indexPath, id obj) {
        NSLog(@"%@", indexPath);
        NSLog(@"%ld", operationType);
        NSLog(@"%@", obj);
    }];
View Code

说明:self.datasource一个初始化了数组,存放数据源;@“pid”数据源中model对象中的标识属性,此处是Person类的中pid属性;changBlock操作时回调的block,  operationType操作类型(增、删、改、刷新),indexPath操作的行, obj操作的类,此处是person对象

2. 在需要执行操作的地方发出通知消息

- (IBAction)delete:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:TableViewOperationDeleteNotification object:self.p];
}
- (IBAction)update:(id)sender {
    self.p.name = [NSString stringWithFormat:@"change_%@", self.p.name];
    [[NSNotificationCenter defaultCenter] postNotificationName:TableViewOperationUpdateNotification object:self.p];
}
View Code

大功告成,用起来是不是非常方便。

具体的源码和demo请访问   

https://github.com/appleboyaug/UITableView-DataChanged

 

posted @ 2015-03-06 19:02  船长_jerry  阅读(8573)  评论(0编辑  收藏  举报