2014-03-19 56 views
0

我試圖從它的原始索引中刪除一個單元格,並把它放在tableview的開頭,我試圖用此代碼做到這一點,但我不能,它崩潰,所以我需要別人的幫助...如何刪除單元格並將其放置在另一個索引中?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    [cell setSwipeGestureWithView:important color:yellowColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState3 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) { 


    NSIndexPath *myindex = [self.tableview indexPathForCell:cell]; 
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

    [self.tableview beginUpdates];   
    [self.tableview moveRowAtIndexPath:myindex toIndexPath:newIndexPath]; 
    [self.tableview endUpdates]; 
    [self.userdefaults setObject:self.tasks forKey:@"tasks"]; 
    [self.userdefaults synchronize]; 

    } 
} 

這是崩潰日誌:

'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]' 

謝謝!

+0

你試過 - [UITableView moveRowAtIndexPath:toIndexPath:]? –

+4

要走出一個肢體,並說你不應該刪除並在cellForRowAtIndexPath方法中添加單元格 –

+0

偉大的@RhythmicFistman完美地工作,但這個新的單元格順序不savig,tableview數據是從一個數組(任務)如何我能解決這個問題嗎?我試着添加更新的代碼 –

回答

1

您在評論中這樣說:

的實現代碼如下數據是從一個陣列(任務)

如果你想在細胞編程方式重新排序,那麼最簡單的事情,你可以可能的做法是重新排序支持數組中的項目,然後調用:

[self.tableview reloadData] 
+0

我同意 - 表格顯示你的數據,它不是你的數據。如果你想更高效,你可以移動單獨的行而不是調用reloadData,但是除非你改變底層的dataSource,否則它不會「粘住」 – Paulw11

+0

當我添加[self.tableview ReloadData]時,移動單元格的功能不工作,我刪除endUpdates和調用reloadData,也不工作:S –

+0

準備好我在ViewWillDissapear添加reloadData和工作很好,謝謝! –

相關問題