更新:在删除细胞之前调整整数!
为什么滑动时会出现以下崩溃要删除?我的tableview行数取决于NSInteger;所以我不明白这里的问题是什么。 通过调整此整数,我没有调整数据源吗?
#import "sendWithQuestionOptionsController.h"
@interface sendWithQuestionOptionsController ()
@property NSInteger NUMBER_OPTIONS;
@end
@implementation sendWithQuestionOptionsController
-(void)viewWillAppear:(BOOL)animated {
self.NUMBER_OPTIONS=4;
SHOULD_SHOW_OPTIONS=YES;
//make the UISegmentedControl.
}
#define MIMIMUM_NUMBER_OPTIONS 2 //be sure to change the UI in all the viewControllers to handle this as well if you end up changing anything.
#define POST_HEIGHT 150
#define OPTION_HEIGHT 50
#define HEADER_SECTION_HEIGHT 20
BOOL SHOULD_SHOW_OPTIONS;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"postCell" forIndexPath:indexPath];
[cell.contentView setBackgroundColor:[UIColor redColor]];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0) {
return 1;
}
if (section==1 ) {
if (SHOULD_SHOW_OPTIONS) {
return self.NUMBER_OPTIONS;
} else{
return 0;
}
}
return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section==0) {
return 0;
}
return HEADER_SECTION_HEIGHT; //for the options header...
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==0) {
return POST_HEIGHT;
}
return OPTION_HEIGHT;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section==0) {
return @""; //no title for the first header (first header isn't shown)
}
if (section==1) {
if (self.NUMBER_OPTIONS==2) {
return @"Options";
} else{
return @"Options (swipe to delete)"; //they can still swipe to delete more.
}
}
return @"";
}
//called when user selects segmented control.
-(void)selectedOptionsInSegmentedControl {
SHOULD_SHOW_OPTIONS=YES;
self.NUMBER_OPTIONS=4;
}
//called when user selects segmented control.
-(void)selectedNoOptionsInSegmentedControl {
SHOULD_SHOW_OPTIONS=NO;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==0) {
return NO;
}
if (indexPath.section==1) {
if (indexPath.row>MIMIMUM_NUMBER_OPTIONS-1) {
return YES;
}
}
return NO;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
self.NUMBER_OPTIONS-=1;
//[self.tableView reloadData];
}
}
@end
答案 0 :(得分:0)
解决方案是首先通过调用self.NUMBER_OPTIONS- = 1更新NSInteger“数据源”;然后删除该行。