array删除报错

楼下如果是妹子那就 2014-10-15 08:12:57
@property (strong, nonatomic) NSMutableArray *dicarray;

-(void)parsingJson
{
NSUserDefaults *Store_Update = [NSUserDefaults standardUserDefaults];
id jsonObject=[Store_Update objectForKey:@"json"];
if ([jsonObject isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:jsonObject];
self.dicarray=[dictionary objectForKey:@"Name"];
}else
{
NSLog(@"ぶしで");
}
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle==UITableViewCellEditingStyleDelete)
{
[self.dicarray removeObjectAtIndex:[indexPath row]];
}
[self.tableview deleteRowsAtIndexPaths:self.dicarray withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView reloadData];
}


走到删除的时候就蹦了,

求诸位指导。
...全文
221 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_22229099 2015-12-15
  • 打赏
  • 举报
回复
谢谢!啊啊啊啊啊啊
yhryyq 2014-10-16
  • 打赏
  • 举报
回复
引用 楼主 ningshuang520 的回复:
@property (strong, nonatomic) NSMutableArray *dicarray;
-(void)parsingJson
{
    NSUserDefaults *Store_Update = [NSUserDefaults standardUserDefaults];
    id jsonObject=[Store_Update objectForKey:@"json"];
    if ([jsonObject isKindOfClass:[NSDictionary  class]])
    {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:jsonObject];
        self.dicarray=[dictionary objectForKey:@"Name"];
    }else
    {
        NSLog(@"ぶしで");
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle==UITableViewCellEditingStyleDelete)
    {
        [self.dicarray removeObjectAtIndex:[indexPath row]];
    }
    [self.tableview deleteRowsAtIndexPaths:self.dicarray withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView reloadData];
}
走到删除的时候就蹦了, 求诸位指导。
以前写demo时候遇到过,从[NSUserDefaults standardUserDefaults]中取出的字典不管你存的时候是什么格式,取出来就变成了NSDictionary而不是NSMutableDictionary,数组同理。
hslinux 2014-10-16
  • 打赏
  • 举报
回复
Show the Breakpoint navigator(项目ios device下面那个左平右尖那个按键),左下角,+,Add Exception Breakpoint。 做完上面的操作,重新debug程序吧,然后再崩溃的会运气不差的话,就会直接定位到出错的代码。
  • 打赏
  • 举报
回复
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(@"ぶしで");
    }
删除的问题已经解决了,取值的时候转一下就好了,这样还是存在问题,删除后再次返回到这个页面数据没更新。
阿七哥 2014-10-16
  • 打赏
  • 举报
回复
楼主,你好。我注意到了NSMutableArray * dicarray;是没有实例化变量的。
  • 打赏
  • 举报
回复
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];
  • 打赏
  • 举报
回复
引用 12 楼 zhanglei5415 的回复:
删除后的数据又更新到NSUserDefaults 中了吗? 如果没有,显示的数据还是原来没更新过的。
请问这个数组再往回更新NSUserDefaults中该怎么操作,求指导。
ReyZhang 2014-10-16
  • 打赏
  • 举报
回复
引用 9 楼 ningshuang520 的回复:
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(@"ぶしで");
    }
删除的问题已经解决了,取值的时候转一下就好了,这样还是存在问题,删除后再次返回到这个页面数据没更新。
删除后的数据又更新到NSUserDefaults 中了吗? 如果没有,显示的数据还是原来没更新过的。
Kamto 2014-10-15
  • 打赏
  • 举报
回复
JSON取的数组应该是可以进行删除操作的
ReyZhang 2014-10-15
  • 打赏
  • 举报
回复
引用 5 楼 ningshuang520 的回复:
[quote=引用 2 楼 zhanglei5415 的回复:] 确保你的self.dicarray 是个可变数组(NSMutableArray).
请问从JSON中取的数组是不能直接进行删除操作的么,这个不知道是不是这个原因。[/quote] 问题在于所有从NSUserDefaults返回的值全部是immutable(不可变)的,所以在修改数组时会出现这个问题。你可以尝试在使用self.dicarray之前先实例化一下
self.dicarray =[[NSMutableArray alloc] initWithCapacity:0];
之后的其它代码不变,再运行试试
  • 打赏
  • 举报
回复
引用 2 楼 zhanglei5415 的回复:
确保你的self.dicarray 是个可变数组(NSMutableArray).
请问从JSON中取的数组是不能直接进行删除操作的么,这个不知道是不是这个原因。
  • 打赏
  • 举报
回复
引用 1 楼 Kamto_6 的回复:
if(editingStyle==UITableViewCellEditingStyleDelete) { [self.dicarray removeObjectAtIndex:[indexPath row]]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } 不需要reloadData吧
额,这个改了还是报这个错,难道是从取的数据的问题?
NSMutableArray *Variablearray=[NSMutableArray arrayWithArray:self.dicarray];
    if(editingStyle==UITableViewCellEditingStyleDelete)
    {
        [Variablearray removeObjectAtIndex:[indexPath row]];
        [self.tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
  • 打赏
  • 举报
回复
引用 2 楼 zhanglei5415 的回复:
确保你的self.dicarray 是个可变数组(NSMutableArray).
self.dicarray的数据是从JSON中取的,我在上面的方法中,取值的时候就用的NSMutableArray,难道是这的问题。
ReyZhang 2014-10-15
  • 打赏
  • 举报
回复
确保你的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
Kamto 2014-10-15
  • 打赏
  • 举报
回复
if(editingStyle==UITableViewCellEditingStyleDelete) { [self.dicarray removeObjectAtIndex:[indexPath row]]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } 不需要reloadData吧

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧