滑动以删除表格单元格时出错

时间:2012-07-29 10:53:14

标签: objective-c ios uitableview

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

这是我的代码来实现这一目标 在.h

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

在.m我有这个代码。

#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:]:发送到不可变对象的mutating方法。

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

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

5 个答案:

答案 0 :(得分:0)

从JSON调用到arrayWithUser的赋值是在viewDidLoad中返回NSArray而不是NSMutableArray。解决了这个问题。

答案 1 :(得分:0)

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

NSArray *rData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
arrayWithUser = [rData mutableCopy];

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

只需替换下面的行

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

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

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

答案 4 :(得分:0)

您在viwDidLoad中使用

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

让我们这样试试

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

self.arrayWithUser = userMutarr;

[userMutarr release];

然后

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