美文网首页
UITableViewDiffableDataSource--i

UITableViewDiffableDataSource--i

作者: Smallwolf_JS | 来源:发表于2021-01-05 15:49 被阅读0次

    今天面试遇到了这个问题,最近一直关注业务,没有关注到新api,所以被问到UITableViewDiffableDataSource第一时间还是一脸懵逼的
    为了以后不遇到这种状况,所以面试完之后立即查了下
    大概看了下没什么难的好像

    
    #import "DiffDataSource.h"
    
    @implementation DiffDataSource
    
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            
            id item = [self itemIdentifierForIndexPath:indexPath];
            
            //这里注意一下,每次调用self.snapshot都会创建新的对象⚠️⚠️⚠️
            NSDiffableDataSourceSnapshot *snapshot = self.snapshot;
            NSDiffableDataSourceSnapshot *snapshot1 = self.snapshot;   //测试⚠️
            NSDiffableDataSourceSnapshot *snapshot2 = self.snapshot;   //测试⚠️
            NSDiffableDataSourceSnapshot *snapshot3 = self.snapshot;   //测试⚠️
            
            //删除的时候不用指定Section,因为每一个item都是唯一的
            [snapshot deleteItemsWithIdentifiers:@[item]];
    
            [self applySnapshot:snapshot animatingDifferences:YES completion:^{
                //
            }];
            
            //不可以再使用tableview的方法删除⚠️⚠️⚠️
            //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            
        } else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
        }
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 20;
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return [NSString stringWithFormat:@"%ld",section];
    }
    
    @end
    

    加载tableView地方的代码

    - (UITableView *)listView{
        if (!_listView) {
            _listView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
            [_listView registerClass:UITableViewCell.class forCellReuseIdentifier:@"Cell"];
            _listView.delegate = self;
            _listView.dataSource = self.dataSource;
        }
        return _listView;
    }
    - (UITableViewDiffableDataSource *)dataSource {
        
        if (!_dataSource) {
            _dataSource = [[DiffDataSource alloc]initWithTableView:self.listView cellProvider:^UITableViewCell * _Nullable(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath, id _Nonnull date) {
                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
            
                NSDate *object = date;
                cell.textLabel.text = [object description];
                return cell;
            }];
        }
        return _dataSource;
    }
    - (void)insertNewObject:(id)sender {
        
        NSDiffableDataSourceSnapshot *snapshot = self.dataSource.snapshot;
        
        //不可以每次都创建新的snapshot⚠️,否则数据都是新的!
        //NSDiffableDataSourceSnapshot *snapshot = [[NSDiffableDataSourceSnapshot alloc] init];
        
        //数据太多了清空一下
        if (snapshot.numberOfItems >= 10) {
            [snapshot deleteAllItems];
            [self.dataSource applySnapshot:snapshot animatingDifferences:YES completion:^{
            }];
            return;
        }
        
        //必须先创建Section才可以插入数据
        if(snapshot.numberOfSections==0)
        {
            [snapshot appendSectionsWithIdentifiers:@[@"1"]];   //SectionIdentifierType不可以重复⚠️
        }else
        {
            if (snapshot.numberOfItems == 5) {
                [snapshot appendSectionsWithIdentifiers:@[@"2"]];  //根据需求添加更多的Section
            }
        }
        
        //往Section中添加数据,默认添加到最后一个Section中
        [snapshot appendItemsWithIdentifiers:@[[NSDate date],[NSDate date]]];  //ItemIdentifierType也不可以重复⚠️
        
        //往指定Section中添加数据
        //[snapshot appendItemsWithIdentifiers:@[[NSDate date]] intoSectionWithIdentifier:@"2"];
        
        [self.dataSource applySnapshot:snapshot animatingDifferences:YES completion:^{
            //
        }];
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"点击了某一个cell%ld",(long)indexPath.row);
    }
    

    我的结论是苹果感觉reloadData不够优雅高效,所以换了一种方式.
    但是注意这个api的要是iOS13+,所以支持低版本iOS系统的app还是绕道吧

    相关文章

      网友评论

          本文标题:UITableViewDiffableDataSource--i

          本文链接:https://www.haomeiwen.com/subject/atcwoktx.html