0

我试图在用户滑动时删除一行。我得到这个错误让我发疯。我花了三个小时试图找出原因。但是,到目前为止,我还没有任何线索。

这是我的代码来实现这一点。
在.h

  #import <UIKit/UIKit.h>
  #import "CustomCell.h"
  @interface FollowersTableViewController : UITableViewController
  @property (nonatomic,strong)NSMutableArray *arrayWithUser ;
  @end

在 .mi 中有这个代码。

#import "FollowersTableViewController.h"
@implementation FollowersTableViewController
@synthesize  arrayWithUser ;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
    -(void)viewDidLoad
    {
        [super viewDidLoad];
        NSDictionary *dicUrlList= [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Urls" ofType:@"plist"]];
        NSString *baseURl = [dicUrlList objectForKey:@"urlWithUser"];
        baseURl = [baseURl stringByAppendingFormat:@"getfollowers"];
        NSURL *urlToGetFollowers = [NSURL URLWithString:baseURl];
        NSURLRequest *request = [NSURLRequest requestWithURL:urlToGetFollowers];
        NSError *error = nil ; 
        NSURLResponse *response = nil ; 
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        arrayWithUser = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

    } 
    - (void)viewDidUnload
    {
        [super viewDidUnload];
    }
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }        
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {

        return 1;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    { 
        return [arrayWithUser count];
    }   
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {  
        static NSString *MyIdentifier = @"Cell";
        CustomCell *customCell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        if (customCell == nil) 
        {
            customCell = [[CustomCell alloc] initWithFrame:CGRectMake(0, 0, 320, 50)] ;
        }
        NSDictionary *dicWithUser = [arrayWithUser objectAtIndex:indexPath.row];
        NSString *photoUrl = [dicWithUser objectForKey:@"profilePhotoUrl"];
        if(![photoUrl isEqualToString:@""])
            [customCell.thumbnail setImageWithURL:[dicWithUser objectForKey:@"profilePhotoUrl"] placeholderImage:[UIImage imageNamed:@"placeholder.png"] ];
        else 
        {
            [customCell.thumbnail setImage:[UIImage imageNamed:@"placeholder.png"]];
        }
        customCell.titleLabel.text = [dicWithUser objectForKey:@"username"];
        UIButton *buttonFollow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [buttonFollow setTitle:@"Follow" forState:UIControlStateNormal];
        CGRect frame = buttonFollow.frame ; 
        frame = CGRectMake(200, 10, 60, 30);
        buttonFollow.frame = frame ;
        buttonFollow.tag = indexPath.row ;
        [buttonFollow addTarget:self action:@selector(followButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        customCell.accessoryView = buttonFollow ;
        return customCell;    
    }   
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 60 ;
    }
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source
            [arrayWithUser removeObjectAtIndex:indexPath.row];
        }    
    }

到目前为止,我可以看到删除按钮,但是当我按下它时,我出现了这个错误

[__NSCFArray removeObjectAtIndex:]:发送到不可变对象的变异方法。

由于我已经使用过NSMutableArray,我不知道为什么会出现此错误?

我已经尝试清理项目。它没有任何区别。

4

5 回答 5

0

从 JSON 调用对 arrayWithUser 的分配在 viewDidLoad 中返回 NSArray 而不是 NSMutableArray。修复它。

于 2012-07-29T10:57:34.540 回答
0

您的 Json 调用返回一个 NSArray。您可以去创建一个 mutableCopy - 这样您就可以使用“removeAtIndex..”方法。

NSArray *rData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
arrayWithUser = [rData mutableCopy];
于 2012-07-29T13:57:25.420 回答
0

实际上,您的 array(arrayWithUser) 强烈指向 JSONObjectWithData 返回的数组,因为您没有返回数组的所有权,因此无法删除其对象。你最好做一件事,拥有那个阵列的所有权。

arrayWithUser = [[NSMutableArray alloc]arrayByAddingObjectsFromArray:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]];
于 2012-07-29T15:39:09.703 回答
0

只需替换以下行

arrayWithUser = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

arrayWithUser = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

来自 Apple 的文档:
NSJSONReadingMutableContainers:指定将数组和字典创建为可变对象。
NSJSONReadingMutableLeaves:指定 JSON 对象图中的叶字符串创建为 NSMutableString 的实例。

于 2013-05-08T14:39:40.957 回答
0

你在 viwDidLoad 中使用过

arrayWithUser = [NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableLeaves 错误:无];

让我们试试这样

NSMutableArray *userMutarr = [NSMutableArray alloc]initWithCapacity:3];

self.arrayWithUser = userMutarr;

[userMutarr 发布];

然后

self.arrayWithUser = [NSJSONSerialization JSONObjectWithData:数据选项:NSJSONReadingMutableLeaves 错误:无];

于 2013-09-11T05:09:41.597 回答