場(chǎng)景
當(dāng)使用deleteRowsAtIndexPaths刪除tableView中某個(gè)section的最后一個(gè)row的時(shí)候,發(fā)生系統(tǒng)崩潰。
原因
- 刪除了某個(gè)section的最后一個(gè)row,但是section卻沒有被刪除,系統(tǒng)就會(huì)發(fā)生UI崩潰。
- 如果修改了數(shù)據(jù)源中的section數(shù)和row數(shù)后調(diào)用tableView的reload方法就不會(huì)發(fā)生UI崩潰。
- 推測(cè)是,如果不使用reload來刷新tableView,就要手動(dòng)去管理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];
// 如果這樣調(diào)用會(huì)發(fā)生崩潰
// [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
if (a.count == 0) { // 要根據(jù)情況直接刪除section或者僅僅刪除row
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
} else {
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
}
@end