Ios [\uu NSCFArray removeObjectAtIndex:]:发送到不可变对象的变异方法';
Hii我是ios开发者中的新手。我想删除tableview中的行。我将实现代码,但当我滑动tableview时,单击“删除”按钮时,显示错误 [\uu\NSCFArray removeObjectAtIndex:]:将变异方法发送到不可变对象的 这是我的截图 这是我的密码Ios [\uu NSCFArray removeObjectAtIndex:]:发送到不可变对象的变异方法';,ios,objective-c,uitableview,Ios,Objective C,Uitableview,Hii我是ios开发者中的新手。我想删除tableview中的行。我将实现代码,但当我滑动tableview时,单击“删除”按钮时,显示错误 [\uu\NSCFArray removeObjectAtIndex:]:将变异方法发送到不可变对象的 这是我的截图 这是我的密码 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { return [cart_dat
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [cart_data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *simpleTableIdentifier = @"cellitem";
cell *cells = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cells == nil) {
cells = [[cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cells.lbl_title.text = [NSString stringWithFormat:@"%@",cart_data[indexPath.row][@"name"]];
cells.lbl_price.text = [NSString stringWithFormat:@"%@",cart_data[indexPath.row][@"price"]];
cells.lbl_qty.text = [NSString stringWithFormat:@"%@",cart_data[indexPath.row][@"qty"]];
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@",cart_data[indexPath.row][@"img_url"]]];
[cells.image setImageWithURL:url placeholderImage:[UIImage imageNamed:@"imagename"] ];
return cells;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[cart_data removeObjectAtIndex:indexPath.row];
[self.table deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.table reloadData];
//add code here for when you hit delete
}
}
帮助我找到任何解决方案…感谢advance错误消息明确指出,
cart_data
是NSArray
而不是预期的NSMutableArray
即使声明(和初始化)是正确的,以后也可能会将不可变数组分配给该属性,从而使其不可变。在这种情况下,需要在NSArray
实例上调用mutableCopy
PS:您可以简化deleteRowsAtIndexPaths
行
...deleteRowsAtIndexPaths:@[indexPath] withRowAnimation...
我猜,
cart\u data
是一个NSArray
而不是一个NSMutableArray
。cart数据是NSMutableArray.bcs。当它们进入服务器时,cart\u数据应该用NSMutableArray声明,并检查是否(cart\u data.count>indexpath.row)->PERFORM DELTEENo\uu nsFarray意味着您必须从nsarray not nsmutable数组中删除对象。你能把分配代码放在这里吗?@DharmeshDhorajiya。可变数组。。。initWithArray
和mutableCopy
是冗余的。其中一个就足够了。