代码之家  ›  专栏  ›  技术社区  ›  arachide

移动UITableCell

  •  0
  • arachide  · 技术社区  · 15 年前

    我用下面的代码移动了一个UITableCell

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    
    
        NSMyObj *newObj=[myArray objectAtIndex:[fromIndexPath row]]  ;//myArray is the array to store NSMyObj
        [myArray removeObjectAtIndex:[fromIndexPath row]];
        [myArray insertObject:newObj atIndex:[toIndexPath row]];//
    
    }
    

    但报告说:

    objc[316]:freed(id):消息保留发送到freed对象=0x3d3d330

    欢迎发表评论

    谢谢

    交互开发

    1 回复  |  直到 15 年前
        1
  •  0
  •   kennytm    15 年前

    你必须暂时 -retain 新对象以避免它成为所有者较少和交易。

    NSMyObj *newObj = [[myArray objectAtIndex:[fromIndexPath row]] retain]; // <---
    [myArray removeObjectAtIndex:[fromIndexPath row]];
    [myArray insertObject:newObj atIndex:[toIndexPath row]];
    [newObj release]; // <---