2010-01-10 51 views
4

我有一個NSMutableArray,並從中加載我的tableview。現在我在UI中有一個Button,它允許用戶刷新多次進入數組的數據。 並且每次都有數據中的新數據我想刷新tableView。 在更新數組後,只是在執行[tableView reloadData]似乎會引起沙灘球。 任何關於什麼是實現這個目標的好方法的想法?用新數據多次清爽NSTableView

而且我一直在考慮看看綁定的方式來從一個數組實現我NSTableView的,但顯示在線使用綁定所有的例子,當他們想要將數據添加到自己的表? 任何人都可以指出我如何使用綁定將數據加載到tableView中?

很抱歉,如果問題是noobie的,我願意看,如果有人可以點我到正確的數據。謝謝:)(我不是在尋找一個快捷方式,只是想從對如何處理這些事情經歷人得到一些教訓諮詢)如果-reloadData是造成皮球幾乎可以肯定意味着

-(IBAction)refreshList:(id)sender 
{ 
//setup array here and sort the array based on one column. This column has 
    identifier 'col1' and it works as expected 


[aTable reloadData]; 
    } 

- (int) numberOfRowsInTableView:(NSTableView *)aTable 
{ // return count of array 
} 

- (id)tableView:(NSTableView *)aTable objectValueForTableColumn: (NSTableColumn *)   
tableColumn row:(int)row 
{ 
//set up arrays here to load data in each column 


} 
- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray 
*)oldDescriptors 
{ 
//sort here when column headers are clicked 
} 

-(IBAction)autorefresh:(id)sender 
    { 

// Here i am trying to reload the array and refresh the tableView. I want to  
    constantly keep refreshing the array and loading the tableView here. The array does 
    get refreshed but I am having trouble loading the tableView. 


    for (int i =0; i<=2;i++) 
    { 
    // reload the array with data first. 
    [aTable reloadData]; 
    i = 1; 

    } 
+0

歡迎SO。請正確標記您的問題:) – skaffman 2010-01-10 22:32:25

回答

1

與該代碼(尤其是你很新穎「而真正的」循環),你會得到一個沙灘球,因爲你永遠不會回到人跑環。要解決使用這樣的代碼建立NSTableView的後,它將運行每1.0秒

NSTimer* timer = [NSTimer timerWithTimeInterval:1.0 
             target:[NSApp delegate] 
             selector:@selector(myReloadData:) 
             userInfo:nil 
             repeats:YES]; 

[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; 

然後創建myReloadData在您的應用程序代理

- (void)reloadMyData:(NSTimer*)ntp 
{ 
    // reload the array with data first. 
    [aTable reloadData]; 
} 
+0

>你的小說「真的」循環 不只是正確的答案,也寫得很好。 – alfwatt 2015-06-01 03:47:03

1

有什麼不對與您的控制器執行NSTableDataSource協議。你需要弄清楚爲什麼會發生並修復它。如果你發佈你的表格數據源代碼,那麼也許我們可以幫你找出原因。

我強烈建議你熟悉的「標準」 NSTableView數據源和代表連看之前的綁定方法。可可綁定是一個相對高級的話題,它非常像你需要更多基本的可可經驗,然後再轉到綁定。

也就是說,這個頁面有一個全面的Cocoa綁定的例子:

http://homepage.mac.com/mmalc/CocoaExamples/controllers.html

更新,因爲你發佈你的代碼:

我必須假設你故意留下在上面的代碼中實現了數據源方法,因爲你發佈的代碼不會在沒有警告的情況下編譯。

您的autorefresh方法是一個無限循環。這將解釋海灘球。您在循環的每次迭代中將i設置爲1,這意味着永遠不會達到結束條件。

但是,使用for環這樣是刷新表格視圖,將阻止主線程一個可怕的,可怕的方式。如果您需要定期重複更新表格視圖,請使用以指定間隔調用的NSTimer

+0

謝謝irsk。我一定會做更多的閱讀。我添加了上面的代碼。有任何想法嗎 ? – cocoacoder 2010-01-11 09:14:08

+0

哎呀,愚蠢的錯誤。得到它了。我實現了NSTimer,並按預期工作。非常感謝。 – cocoacoder 2010-01-12 07:27:52