iOS多级列表实现代码

2020-01-17 22:09:03于海丽

 

但在刷新中会报错

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 2 from section 0 which only contains 2 rows before the update'

推测原因是 current Cell在刷新时的numberOfRowsInSection和刷新insert or del的cell时numberOfRowsInSection不一致导致 。然后尝试current cell和其他cell分别刷新,完美刷新。


[_reloadArray removeAllObjects];
 [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

 if (currentNode.isExpand) {
  //expand
  [self expandNodesForParentID:currentNode.childrenID insertIndex:indexPath.row];
  [tableView insertRowsAtIndexPaths:_reloadArray withRowAnimation:UITableViewRowAnimationNone];
 }else{
  //fold
  [self foldNodesForLevel:currentNode.level currentIndex:indexPath.row];
   [tableView deleteRowsAtIndexPaths:_reloadArray withRowAnimation:UITableViewRowAnimationNone];
 }

2.怎么保存节点历史状态

当文件级层比较多时,有时希望能关掉层级后再打开时还能保留子层级的打开状态。我们可以会给每一个node一个是否展开的属性,当fold时只修改currentNode的expand属性,expand时对子节点序isexpand=YES的进行遍历插入。


//expand
- (NSUInteger)expandNodesForParentID:(NSString*)parentID insertIndex:(NSUInteger)insertIndex{

 for (int i = 0 ; i<_nodes.count;i++) {
  YKNodeModel *node = _nodes[i];
  if ([node.parentID isEqualToString:parentID]) {
   if (!self.isPreservation) {
    node.expand = NO;
   }
   insertIndex++;
   [_tempNodes insertObject:node atIndex:insertIndex];
   [_reloadArray addObject:[NSIndexPath indexPathForRow:insertIndex inSection:0]];//need reload nodes

   if (node.isExpand) {
    insertIndex = [self expandNodesForParentID:node.childrenID insertIndex:insertIndex];
   }
  }
 }

 return insertIndex;
}

demo地址:
https://www.easck.com/p>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。


注:相关教程知识阅读请移步到IOS开发频道。