我在iPad应用程序中实现了可扩展和可折叠的UITableView。用户可以添加或删除部分。我正在删除部分标题上放置的按钮标记基础上的部分。如果我删除从最后一个到第一个的部分然后它是好的否则应用程序崩溃第二或第三删除。因为当我删除任何部分时,它不会刷新UIButton的标签。 这是我的代码
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
/*
Create the section header views lazily.
*/
// delete button.
delButton = [UIButton buttonWithType:UIButtonTypeCustom];
delButton.frame = CGRectMake(600.0, 5.0, 35.0, 35.0);
[delButton setImage:[UIImage imageNamed:@"trash.png"] forState:UIControlStateNormal];
delButton.tag = section;
[delButton addTarget:self action:@selector(deleteSectionHeader:) forControlEvents:UIControlEventTouchUpInside];
Section *aSection=[sectionArray objectAtIndex:section];
if (!aSection.sectionHeaderView) {
aSection.sectionHeaderView = [[SectionHeaderView alloc] initWithFrame:CGRectMake(0.0, 0.0, subActivityTableView.bounds.size.width, HEADER_HEIGHT) title:aSection.sectionHeader section:section delegate:self];
[aSection.sectionHeaderView addSubview:delButton];
}
return aSection.sectionHeaderView;
}
-(void)deleteSectionHeader:(UIButton*)sender{
NSInteger section = sender.tag;
[sectionArray removeObjectAtIndex:section];
[subActivityTableView deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
// reload sections to get the new titles and tags
NSInteger sectionCount = [sectionArray count];
NSIndexSet *indexes = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, sectionCount)];
[subActivityTableView reloadSections:indexes withRowAnimation:UITableViewRowAnimationNone];
[subActivityTableView reloadData];
}