场景
当使用deleteRowsAtIndexPaths删除tableView中某个section的最后一个row的时候,发生系统崩溃。
原因
- 删除了某个section的最后一个row,但是section却没有被删除,系统就会发生UI崩溃。
- 如果修改了数据源中的section数和row数后调用tableView的reload方法就不会发生UI崩溃。
- 推测是,如果不使用reload来刷新tableView,就要手动去管理seciton和row。
源码
#import "ViewController.h"
#define RGB(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong) NSMutableArray *data;
@property(nonatomic, strong) NSMutableArray *section1;
@property(nonatomic, strong) NSMutableArray *section2;
@property(nonatomic, strong) NSMutableArray *section3;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupViews];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setupViews {
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];
[self.view addSubview:tableView];
tableView.sectionHeaderHeight = 20;
tableView.rowHeight = 44;
tableView.dataSource = self;
tableView.delegate = self;
_section1 = [NSMutableArray arrayWithObjects:@1, @2, nil];
_section2 = [NSMutableArray arrayWithObjects:@1, @2, nil];
_section3 = [NSMutableArray arrayWithObjects:@1, nil];
_data = [NSMutableArray arrayWithObjects:_section1, _section2, _section3, nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int count = 0;
if (_section1.count) {
count ++;
}
if (_section2.count) {
count ++;
}
if (_section3.count) {
count ++;
}
return count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *a = _data[section];
return a.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.backgroundColor = RGB(arc4random()%255,arc4random()%255,arc4random()%255);
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
UILabel *label = [[UILabel alloc] initWithFrame:view.bounds];
label.text = @"header";
label.font = [UIFont systemFontOfSize:16];
label.textColor = [UIColor blackColor];
[view addSubview:label];
return view;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray *a = _data[indexPath.section];
[a removeObjectAtIndex:indexPath.row];
// 如果这样调用会发生崩溃
// [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
if (a.count == 0) { // 要根据情况直接删除section或者仅仅删除row
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
} else {
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
}
@end
网友评论