gpt4 book ai didi

ios - [__NSCFArray removeObjectAtIndex :]: mutating method sent to immutable object'

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:30:55 29 4
gpt4 key购买 nike

你好我是 ios 开发人员的新手。我想删除 tableview 中的行。我将实现代码但是当我滑动 tableview 时点击删除按钮而不是显示错误

[__NSCFArray removeObjectAtIndex:]:发送到不可变对象(immutable对象)的变异方法'

这是我的截图

enter image description here

这是我的代码

- (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
}
}

帮助我任何解决方案...感谢提前

最佳答案

错误消息清楚地表明 cart_dataNSArray 而不是预期的 NSMutableArray

即使声明(和初始化)是正确的,也可能会发生稍后将不可变数组分配给该属性使其不可变的情况。在这种情况下,您需要在 NSArray 实例上调用 mutableCopy

PS:你可以简化deleteRowsAtIndexPaths

...deleteRowsAtIndexPaths:@[indexPath] withRowAnimation...

关于ios - [__NSCFArray removeObjectAtIndex :]: mutating method sent to immutable object',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37628256/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com