刷卡删除表格单元格时出错
我试图在用户滑动时删除一行。我得到这个错误让我疯狂。我花了最近三个小时试图找出原因。但是,我目前还没有任何线索。刷卡删除表格单元格时出错
这是我的代码来完成。
在.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
,我不知道,为什么我得到这个错误?
我已经尝试清理项目。它没有任何区别。
你的Json调用返回一个NSArray。你可以去创建一个mutableCopy - 这样你就可以使用“removeAtIndex ..”方法。
NSArray *rData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
arrayWithUser = [rData mutableCopy];
从JSON调用向arrayWithUser的赋值是返回一个NSArray而不是viewDidLoad中的NSMutableArray。修复。
接收到的JSON数据来自JAVA使用Hibernate和Spring框架编写的服务器端代码。因此,我无法触及任何这些代码。你有其他建议吗? – limon 2012-07-29 11:11:38
其实,你的阵列(arrayWithUser)强烈指向由JSONObjectWithData阵列的回报,因为你没有返回数组的所有权,则可以不删除其对象。 你最好做一件事,取得该阵列的所有权。
arrayWithUser = [[NSMutableArray alloc]arrayByAddingObjectsFromArray:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]];
只需更换下面一行
arrayWithUser = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
与
arrayWithUser = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
从苹果公司的文档:
NSJSONReadingMutableContainers:指定数组和字典作为可变对象创建。
NSJSONReadingMutableLeaves:指定将JSON对象图中的叶子字符串创建为NSMutableString的实例。
您在viwDidLoad
使用arrayWithUser = [NSJSONSerialization JSONObjectWithData:数据选择:NSJSONReadingMutableLeaves误差:无];
让我们试着这样
的NSMutableArray * userMutarr = [NSMutableArray里的alloc] initWithCapacity:3];
自我。arrayWithUser = userMutarr;
[userMutarr release];
然后
self.arrayWithUser = [NSJSONSerialization JSONObjectWithData:数据选择:NSJSONReadingMutableLeaves误差:无];
我得到它的工作感谢你。 – limon 2012-07-29 16:41:20