由于 ex 访问不当,iphonesdk 值从数组中删除
我已经实现了这段代码,当我尝试添加对象时,它工作正常,但是当我尝试删除对象时,它给了我 exc_bad_access ,我尝试通过放置断点来找出原因,但我仍然无法得到原因。请帮我。
在.h文件中
BOOL prayValues[1000];
BOOL praiseValues[1000];
IBOutlet UITableView *prayTable;
IBOutlet UITableView *praiseTable;
NSMutableArray *publishingMyPosts;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"sterrrr----%@",str);
if (tableView == myTableView) {
NSLog(@"did select in tableview");
}
if (jsonRequestChecking==2) {
UITableViewCell *thisCell1 = [prayTable cellForRowAtIndexPath:indexPath];
UITableViewCell *thisCell2 = [praiseTable cellForRowAtIndexPath:indexPath];
if (tableView == prayTable) {
NSLog(@"did select in prayTable");
prayValues[indexPath.row] = !prayValues[indexPath.row];
if (prayValues[indexPath.row]) {
thisCell1.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(@"this cell1 text %@",thisCell1.textLabel.text);
str=[NSString stringWithFormat:@"%@",[[publicArray objectAtIndex:indexPath.row] objectForKey:@"id"]];
if (([publishingMyPosts containsObject:str] == NO)){
[publishingMyPosts addObject:str];
// NSLog(@"publishing my post %@",publishingMyPosts);
}
} else {
thisCell1.accessoryType = UITableViewCellAccessoryNone;
[publishingMyPosts removeObject:str];
//NSLog(@"publishing my post %@",publishingMyPosts);
}
}
if (tableView == praiseTable) {
NSLog(@"did select in praiseTable");
praiseValues[indexPath.row] = !praiseValues[indexPath.row];
if (praiseValues[indexPath.row]) {
str1=[NSString stringWithFormat:@"%@",[[privateArray objectAtIndex:indexPath.row] objectForKey:@"id"]];
thisCell2.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(@"this cell2 text %@",thisCell2.textLabel.text);
if (([publishingMyPosts containsObject:str1] == NO)){
[publishingMyPosts addObject:str1];
// NSLog(@"publishing my post %@",publishingMyPosts);
}
} else {
thisCell2.accessoryType = UITableViewCellAccessoryNone;
[publishingMyPosts removeObject:str1];
//NSLog(@"publishing my post %@",publishingMyPosts);
}
}
}
NSLog(@"publishing my post %@",publishingMyPosts);
}
i had implemented this code, when i try to add objects it works fine but when i try to remove object it gave me exc_bad_access, i try to find out through putting break points but still i cannot get the reason of this. please help me.
in .h file
BOOL prayValues[1000];
BOOL praiseValues[1000];
IBOutlet UITableView *prayTable;
IBOutlet UITableView *praiseTable;
NSMutableArray *publishingMyPosts;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"sterrrr----%@",str);
if (tableView == myTableView) {
NSLog(@"did select in tableview");
}
if (jsonRequestChecking==2) {
UITableViewCell *thisCell1 = [prayTable cellForRowAtIndexPath:indexPath];
UITableViewCell *thisCell2 = [praiseTable cellForRowAtIndexPath:indexPath];
if (tableView == prayTable) {
NSLog(@"did select in prayTable");
prayValues[indexPath.row] = !prayValues[indexPath.row];
if (prayValues[indexPath.row]) {
thisCell1.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(@"this cell1 text %@",thisCell1.textLabel.text);
str=[NSString stringWithFormat:@"%@",[[publicArray objectAtIndex:indexPath.row] objectForKey:@"id"]];
if (([publishingMyPosts containsObject:str] == NO)){
[publishingMyPosts addObject:str];
// NSLog(@"publishing my post %@",publishingMyPosts);
}
} else {
thisCell1.accessoryType = UITableViewCellAccessoryNone;
[publishingMyPosts removeObject:str];
//NSLog(@"publishing my post %@",publishingMyPosts);
}
}
if (tableView == praiseTable) {
NSLog(@"did select in praiseTable");
praiseValues[indexPath.row] = !praiseValues[indexPath.row];
if (praiseValues[indexPath.row]) {
str1=[NSString stringWithFormat:@"%@",[[privateArray objectAtIndex:indexPath.row] objectForKey:@"id"]];
thisCell2.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(@"this cell2 text %@",thisCell2.textLabel.text);
if (([publishingMyPosts containsObject:str1] == NO)){
[publishingMyPosts addObject:str1];
// NSLog(@"publishing my post %@",publishingMyPosts);
}
} else {
thisCell2.accessoryType = UITableViewCellAccessoryNone;
[publishingMyPosts removeObject:str1];
//NSLog(@"publishing my post %@",publishingMyPosts);
}
}
}
NSLog(@"publishing my post %@",publishingMyPosts);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的indexpath.row看起来是一个问题,希望这可能有效:
your indexpath.row is look like to be an issue, hope this may work :
像这样声明你的 str...
NSString *str = [[ NSString StringWithFormat:@"your string "] keep];
或编写你的 str @property (nonatomic,retain) 和 @synthesize,因为有时 String 会自动释放,因此可以写入保留。我认为这对你有帮助......
Declare your str Like this...
NSString *str = [[ NSString StringWithFormat:@"your string "] retain];
or Write your str @property (nonatomic,retain) and @synthesize, becoz some time String being autoreleased so for that be write retain. I think it helps u.....
我认为发生这种情况是因为您没有通过删除相应的行来更新 uitableview 。因此表视图尝试加载您刚刚删除的数据并崩溃。您应该在removeObject 之后立即调用DeleteRowsAtIndexPaths,或者在函数末尾调用reloadData。
I think this happens because you're not updating the uitableview by deleting the corresponding row. So the table view tries to load the data you've just removed and crashes. You should call DeleteRowsAtIndexPaths right after the removeObject or alternatively call reloadData at the end of the function.