2012-01-13 47 views
1

我在我的主視圖(這是一個Web瀏覽器)中的彈出窗口中有一個tableview。每當用戶訪問一個網頁時,我都希望它將網頁添加到最近訪問過的網站列表中。我將代碼設置爲將URL作爲字符串添加到作爲表視圖的數據源的數組中。表格視圖僅顯示訪問的第一個網站,並且不會顯示任何過去的內容。但是我知道這些站點正在被添加到數組中,因爲數組在添加後顯示了使用NSLog語句的站點。但是他們仍然不會出現在桌子上。誰能幫我?將對象添加到數組後,tableview將不會更新

編輯:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    // Return the number of rows in the section. 
    return [recentlyVisitedUrls count]; 
} 
+1

你在調用'[tableView reloadData]'? – 2012-01-13 21:12:46

+0

是的,但它刪除了之前有過的數據 – Slayter 2012-01-13 22:06:26

回答

2

另外,還要確保你從– tableView:numberOfRowsInSection:

2

嘗試

[tableView reloadTable]; 

你添加新的數據之後。

+0

當我這樣做時,我必須在添加數據之前將其放入並且它以[tableView reloadData]的形式出現;以前的數據也消失了 – Slayter 2012-01-13 22:03:24

+0

顯示numberOfSectionsInTableView:,numberOfRowsInSection:和cellForRowAtIndexPath:的代理方法。爲了格式化的目的,輸入您的原始問題。 – Jim 2012-01-13 23:09:33

+0

我已更新問題 – Slayter 2012-01-13 23:17:05

0

返回你的陣列的正確長度如果您wan't動畫更新,您可以撥打:

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO]

如果你需要的動畫,它會請更好地閱讀:Table View Programming Guide for iOS,您需要Batch insert…部分。

0

如果你有一張大桌子,[tableView reloadData]可能並不理想。它也會將您的視圖重置到表格的開頭,這可能會或可能不需要。你可以嘗試這樣的:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_model.recordCount - 1 inSection:0]; 
[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO]; 

順便說一句,_model是示例中表的數據委託類。

另外,請確保數據委託在插入之前更新其總計數,否則您的應用程序將在insertRowsAtIndexPaths上崩潰。