gpt4 book ai didi

ios - 使用 UITableView 时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 11:48:24 25 4
gpt4 key购买 nike

在第一个屏幕上,用户输入数据并使用 prepareforsegue 方法在第二个 ViewController 中显示它。在第二个屏幕上,用户选择多行进行删除,然后用户再次选择剩余的行进行删除应用程序崩溃。这是我的代码

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray removeObjectsAtIndexes:]: index 5 in index set beyond bounds [0 .. 4]'

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return mAryValue.count;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * strIdent=@"id1Cell";

UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:strIdent];

if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIdent];
}

cell.textLabel.text=[mAryValue objectAtIndex:indexPath.row];

return cell;

}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing)
{
return;
}

[_tblView deselectRowAtIndexPath:[_tblView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [_tblView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[mArySel addObject:indexPath];
}
else if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
[mArySel removeObjectIdenticalTo:indexPath];
}
}

-(void)getData:(NSMutableArray *)userValues
{
mAryValue=userValues;
}

- (IBAction)btnDelete:(id)sender
{
if (mAryValue==nil || mAryValue.count==0)
{

UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Alert"
message:@"Please Enter Value"
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];

}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];

}];

[alert addAction:ok];
[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:nil];
}
else
{
NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
for (NSIndexPath *indexPath in mArySel)
{
[indicesToDelete addIndex:indexPath.row];
}

if (!(indicesToDelete==nil) || !(indicesToDelete.count==0))
{
[mAryValue removeObjectsAtIndexes:indicesToDelete];

}
[_tblView reloadData];
}


}

请给我建议。谢谢。

最佳答案

[NSMutableArray removeObjectsAtIndexes:]: index 5 in index set beyond bounds [0 .. 4]'

您正试图从您的数组 的索引处移除一个不存在的对象。如错误所述,您要删除 objectAtIndex:5,但您的数组只有 4 个项目。

[mAryValue removeObjectsAtIndexes:indicesToDelete];

关于ios - 使用 UITableView 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42294603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com