您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

UITableView 拖動排序:從 UI 到資料模型同步

編輯:iOS 開發綜合

UITableView 的拖動排序,本質上是編輯模式下的 moveRow 行為,但也需要同步更新資料來源。

步驟

  1. 將 tableView.editing 設為 YES。
  2. 實作 editingStyleForRowAtIndexPath 並回傳 UITableViewCellEditingStyleNone。
  3. 實作 moveRowAtIndexPath:toIndexPath:。
  4. 在代理方法中更新 mutable array。
[_tableView setEditing:flag animated:YES];
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    Goods *goods = _goodsAry[sourceIndexPath.row];
    [_goodsAry removeObject:goods];
    [_goodsAry insertObject:goods atIndex:destinationIndexPath.row];
}

這樣畫面排序與資料模型才會一致。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved