2009-08-04 47 views
1

我相信這是一件容易的事情。我對客觀C非常陌生(找到了一份老闆打算外包的工作),並且可以使用我可以獲得的所有幫助。請在這裏指出我正確的方向。基本上我想要建立的是這樣的:將UITableView行移動到列表頂部時

我有一個電話號碼列表,根據需要擴展到多行。我填充沒有問題,並且列表工作正常,但我希望列表中最近選定的所有項目都能夠在頂部更容易地訪問。所以,無論何時用戶「didSelectRowAtIndexPath」,我希望該行移動到頂部。

我只是知道這是超級簡單...但我還沒有找到解決方案。不要着急。我只是想自己解決問題。提前致謝!

回答

3

元素的順序是數據源(您的代碼)的責任,而不是表視圖。如果你想重新排序,那麼你需要重新排序你用來驅動-cellForRowAtIndexPath:的數據結構。如果你想要動畫,我不相信還有一個好的動畫。你可以做的最好的(容易)是執行刪除和插入。有關如何爲其設置動畫的詳細信息,請參見Batch Insertion and Deletion of Rows and Sections。幾個月前,我向UITableView開發者詢問OS2.x中這些動畫的一般性問題和脆弱性,他們向我保證,在OS3中有很多工作要改進。所以希望你可以按照文檔,它不應該崩潰了。

0

UITableView中有一個名爲方法

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

我相信你確實有,雖然寫的代碼,我不知道,因爲我從來沒有帶桌子的工作(我是新和) 。

2

沒有能力從表中的一個位置移除一行到另一個位置。它可以通過刪除的行,然後將其放回在新的位置進行仿真:

// Remove the row from your datasource here 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:sourcePath] withRowAnimation:UITableViewRowAnimationLeft]; 
// Add the row back into the datasource here 
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:destPath] withRowAnimation:UITableViewRowAnimationLeft]; 

替換正確的命令的意見更新您的數據源

0

試試這個(工作代碼):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

if(indexPath.row == 0) return; 

NSString * selectedNum = [numbers objectAtIndex:indexPath.row]; // NSMutableArray* numbers = dataStore 
[selectedNum retain]; 

[numbers removeObjectAtIndex:indexPath.row]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
      withRowAnimation:UITableViewRowAnimationLeft]; 
[numbers insertObject:selectedNum atIndex:0]; 
[tableView insertRowsAtIndexPaths:[NSArray 
      arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] 
      withRowAnimation:UITableViewRowAnimationLeft]; 

[selectedNum release]; 

}

+0

嗨@Tomas,你的代碼的工作,但我們可以有這樣明確的應用動畫效果 – Ranjit 2012-08-02 11:04:25