Cocoa 从NSMutableArray中删除选定的行索引对象并添加到另一个NSMutableArray
我用NSMutableArray中的数据填充了tableview,一切正常。当我选择一个单元格(didSelectRowAtIndex)时,该项将从数组中删除,就像它应该做的那样。现在,我希望将这个完全相同的对象添加到另一个NSMutableArray中 简而言之:从数组01中删除选定对象,然后将选定对象添加到数组02Cocoa 从NSMutableArray中删除选定的行索引对象并添加到另一个NSMutableArray,cocoa,nsmutablearray,Cocoa,Nsmutablearray,我用NSMutableArray中的数据填充了tableview,一切正常。当我选择一个单元格(didSelectRowAtIndex)时,该项将从数组中删除,就像它应该做的那样。现在,我希望将这个完全相同的对象添加到另一个NSMutableArray中 简而言之:从数组01中删除选定对象,然后将选定对象添加到数组02 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPa
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_array01 removeObjectAtIndex:indexPath.row];
[_array02 addobject?????];
}
在将对象从第一个数组中删除之前,需要将其添加到第二个数组中。以下是一种使用两行代码实现此目的的方法:
[_array02 addObject:[_array01 objectAtIndex:indexPath.row]];
[_array01 removeObjectAtIndex:indexPath.row];
在更复杂的情况下,您可能需要对对象执行某些操作,您可以取而代之的是获取对该对象的引用并将其移动到另一个数组,如下所示:
id myObject = [_array01 objectAtIndex:indexPath.row];
[myObject setTitle:@"new title"]; // example of modifying the object before moving it
[_array02 addObject:myObject];
[_array01 removeObjectAtIndex:indexPath.row];
注意,在这里的第二个示例中,根据Objective-C内存管理规则,您不是myObject
的所有者。你只是有一个参考它。myObject
实际上由\u array01
拥有,然后由\u array02
拥有