2012-07-29 52 views
0

我試圖在用戶滑動時刪除一行。我得到這個錯誤讓我瘋狂。我花了最近三個小時試圖找出原因。但是,我目前還沒有任何線索。刷卡刪除表格單元格時出錯

這是我的代碼來完成。
在.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 method sent to immutable object.

因爲我已經使用了NSMutableArray,我不知道,爲什麼我得到這個錯誤?

我已經嘗試清理項目。它沒有任何區別。

回答

0

你的Json調用返回一個NSArray。你可以去創建一個mutableCopy - 這樣你就可以使用「removeAtIndex ..」方法。

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

我得到它的工作感謝你。 – limon 2012-07-29 16:41:20

0

從JSON調用向arrayWithUser的賦值是返回一個NSArray而不是viewDidLoad中的NSMutableArray。修復。

+0

接收到的JSON數據來自JAVA使用Hibernate和Spring框架編寫的服務器端代碼。因此,我無法觸及任何這些代碼。你有其他建議嗎? – limon 2012-07-29 11:11:38

0

其實,你的陣列(arrayWithUser)強烈指向由JSONObjectWithData陣列的回報,因爲你沒有返回數組的所有權,則可以不刪除其對象。 你最好做一件事,取得該陣列的所有權。

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

只需更換下面一行

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

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

從蘋果公司的文檔:
NSJSONReadingMutableContainers:指定數組和字典作爲可變對象創建。
NSJSONReadingMutableLeaves:指定將JSON對象圖中的葉子字符串創建爲NSMutableString的實例。

0

您在viwDidLoad

使用

arrayWithUser = [NSJSONSerialization JSONObjectWithData:數據選擇:NSJSONReadingMutableLeaves誤差:無];

讓我們試着這樣

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

自我。arrayWithUser = userMutarr;

[userMutarr release];

然後

self.arrayWithUser = [NSJSONSerialization JSONObjectWithData:數據選擇:NSJSONReadingMutableLeaves誤差:無];