今天面試遇到了這個問題,最近一直關注業務,沒有關注到新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還是繞道吧