array删除报错 本帖最后由 ningshuang520 于 2014-10-15 20:17:26 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 if(editingStyle==UITableViewCellEditingStyleDelete) { [self.dicarray removeObjectAtIndex:[indexPath row]]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }不需要reloadData吧 确保你的self.dicarray 是个可变数组(NSMutableArray). 删除时可这样if(editingStyle==UITableViewCellEditingStyleDelete) { [self.dicarray removeObjectAtIndex:[indexPath row]]; [self.tableView beginUpdates]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tableView endUpdates]; }在删除行是有动画效果,如果你的数据源及UI都实际已经移除掉了该行,后面也就没必要再重新reloadData self.dicarray的数据是从JSON中取的,我在上面的方法中,取值的时候就用的NSMutableArray,难道是这的问题。 额,这个改了还是报这个错,难道是从取的数据的问题?NSMutableArray *Variablearray=[NSMutableArray arrayWithArray:self.dicarray]; if(editingStyle==UITableViewCellEditingStyleDelete) { [Variablearray removeObjectAtIndex:[indexPath row]]; [self.tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } 请问从JSON中取的数组是不能直接进行删除操作的么,这个不知道是不是这个原因。 请问从JSON中取的数组是不能直接进行删除操作的么,这个不知道是不是这个原因。问题在于所有从NSUserDefaults返回的值全部是immutable(不可变)的,所以在修改数组时会出现这个问题。你可以尝试在使用self.dicarray之前先实例化一下self.dicarray =[[NSMutableArray alloc] initWithCapacity:0];之后的其它代码不变,再运行试试 JSON取的数组应该是可以进行删除操作的 楼主,你好。我注意到了NSMutableArray * dicarray;是没有实例化变量的。 NSUserDefaults *Store_Update = [NSUserDefaults standardUserDefaults]; id jsonObject=[Store_Update objectForKey:@"json"]; if ([jsonObject isKindOfClass:[NSDictionary class]]) { NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:jsonObject]; NSMutableArray *dicarray=[dictionary objectForKey:@"Name"]; self.Variablearray=[NSMutableArray arrayWithArray:dicarray]; }else { NSLog(@"ぶしで"); }删除的问题已经解决了,取值的时候转一下就好了,这样还是存在问题,删除后再次返回到这个页面数据没更新。 Show the Breakpoint navigator(项目ios device下面那个左平右尖那个按键),左下角,+,Add Exception Breakpoint。做完上面的操作,重新debug程序吧,然后再崩溃的会运气不差的话,就会直接定位到出错的代码。 以前写demo时候遇到过,从[NSUserDefaults standardUserDefaults]中取出的字典不管你存的时候是什么格式,取出来就变成了NSDictionary而不是NSMutableDictionary,数组同理。 删除后的数据又更新到NSUserDefaults 中了吗? 如果没有,显示的数据还是原来没更新过的。 请问这个数组再往回更新NSUserDefaults中该怎么操作,求指导。 ok,解决了。 NSMutableDictionary *jsdiction=[[NSMutableDictionary alloc]init]; [jsdiction setValue:self.Variablearray forKey:@"Name"]; NSLog(@"======jsdiction:%@\nVariablearray:%@",jsdiction,self.Variablearray); NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:jsdiction forKey:@"json"]; [userDefaults synchronize]; ios socket 协议解析 NSTemporaryDirectory()的问题 受够了XCode中可笑的IOS模拟器 【怎么把图片放入一个指定的相册? 问个关于99美金的弱弱的问题 【精】IOS文件操作 xmpp在IOS平台的应用 新建工程出现 *** Please tell me who you are. Run [急]数据提取问题 求大神帮忙 ios更新上包的问题 急急急!!!同一个tableViewController的headerView上有个segmentControl,想用来实现本页面上tableView的切换! 请教关于 Struct 结构体的问题
{
[self.dicarray removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
不需要reloadData吧
if(editingStyle==UITableViewCellEditingStyleDelete)
{
[self.dicarray removeObjectAtIndex:[indexPath row]];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}在删除行是有动画效果,如果你的数据源及UI都实际已经移除掉了该行,后面也就没必要再重新reloadData
NSMutableArray *Variablearray=[NSMutableArray arrayWithArray:self.dicarray];
if(editingStyle==UITableViewCellEditingStyleDelete)
{
[Variablearray removeObjectAtIndex:[indexPath row]];
[self.tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
问题在于所有从NSUserDefaults返回的值全部是immutable(不可变)的,所以在修改数组时会出现这个问题。你可以尝试在使用self.dicarray之前先实例化一下
self.dicarray =[[NSMutableArray alloc] initWithCapacity:0];
之后的其它代码不变,再运行试试
NSUserDefaults *Store_Update = [NSUserDefaults standardUserDefaults];
id jsonObject=[Store_Update objectForKey:@"json"];
if ([jsonObject isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:jsonObject];
NSMutableArray *dicarray=[dictionary objectForKey:@"Name"];
self.Variablearray=[NSMutableArray arrayWithArray:dicarray];
}else
{
NSLog(@"ぶしで");
}
删除的问题已经解决了,取值的时候转一下就好了,这样还是存在问题,删除后再次返回到这个页面数据没更新。
ok,解决了。 NSMutableDictionary *jsdiction=[[NSMutableDictionary alloc]init];
[jsdiction setValue:self.Variablearray forKey:@"Name"];
NSLog(@"======jsdiction:%@\nVariablearray:%@",jsdiction,self.Variablearray);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:jsdiction forKey:@"json"];
[userDefaults synchronize];