RGArray 1.3.0

RGArray 1.3.0

Renge 维护。



RGArray 1.3.0

  • 作者
  • RengeRenge

RGArray

RGArray 继承自 NSMutableArray,可以为内部元素的更改(插入、删除、替换、排序)提供回调,包括特定更改位置

安装

通过将以下内容添加到您的 Podfile 中使用 CocoaPods 进行安装

pod 'RGArray'

使用

导入

#import <RGArray/RGArray.h>

RGArrayChangeDelegate

@interface ViewController () <UITableViewDataSource, UITableViewDelegate, RGArrayChangeDelegate>
@property (nonatomic, strong) RGArray <NSString *> *array;
@end
  
- (void)init {
  self.array = [RGArray arrayWithObjects:@"1", @"6", @"4", nil];
  [self.array addDelegate:self];
}

#pragma mark - RGArrayChangeDelegate

- (void)changeArray:(nonnull RGArray *)array change:(nonnull RGArrayChange *)change {
  NSLog(@"%@", change);
  [self.tableView beginUpdates];
  [self.tableView deleteRowsAtIndexPaths:change.deletionIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
  [self.tableView insertRowsAtIndexPaths:change.insertionsIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
  [self.tableView reloadRowsAtIndexPaths:change.modificationIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
  [self.tableView endUpdates];
}

逐步回调

有时候,我们不能使用 "reloadRowsAtIndexPaths" 来刷新列表,因为在列表具有选择状态时,刷新后所选状态将丢失。

此时,您需要取出一个特定的单元格进行修改。

self.array.changeByStep = YES;
#pragma mark - RGArrayChangeDelegate

- (void)changeArray:(RGArray *)array change:(RGArrayChange *)change {
  NSLog(@"change: %@", change);
  if (change.deletions.count || change.insertions.count) {
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:change.deletionIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView insertRowsAtIndexPaths:change.insertionsIndexPaths withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
  } else {
    [change.modificationIndexPaths enumerateObjectsUsingBlock:^(NSIndexPath * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
      UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:obj];
      if (cell) {
        [self configCell:cell withObj:array[obj.row]];
      }
    }];
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellID" forIndexPath:indexPath];
  NSString *obj = self.array[indexPath.row];
  [self configCell:cell withObj:obj];
  return cell;
}

- (void)configCell:(UITableViewCell *)cell withObj:(NSString *)obj {
  cell.textLabel.text = obj;
}

⚠️在这种情况下,如果您不使用逐步回调,则会在列表中引起一些异常。

为元素设置比较规则

  • 为数组设置特殊规则
[self.array setEqualRule:^BOOL(NSString * _Nonnull obj, NSString * _Nonnull other) {
	return [obj isEqualToString:other];
}];
[self.array setModifyRule:^BOOL(NSString * _Nonnull old, NSString * _Nonnull young) {
  return ![old isEqualToString:young];
}];
  • 实现 RGChangeProtocol 对元素进行操作
#pragma mark - RGChangeProtocol

- (BOOL)rg_arrayObjIsEqual:(id)object {
    if ([object isKindOfClass:self.class]) {
        return [NSString rg_equalString:self.iotId toString:[object iotId]];
    }
    return NO;
}

- (BOOL)rg_arrayObjHasModification:(id)old {
    if ([old isKindOfClass:self.class]) {
        BOOL equal = self.status == [old status] &&
        [NSString rg_equalString:self.nickName toString:[old nickName]];
        return !equal;
    }
    return NO;
}

演示

  • 简单案例

    1

  • 复杂案例

    随机生成一个新数组

    2