2012-05-27 47 views
2

我有一個歷史記錄頁面,這是一個包含5行的UItableview。我已經原型小區設置到我想要的規格,並加入這段文字對應的historyviewcontroller.h文件:沒有出現在桌面視圖中的單元格

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 5; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryItem"]; 
return cell; 
} 

我沒有看到任何細胞,當我運行的應用程序。我明顯錯過了一些東西,但我看不出什麼。

回答

5

您需要實際創建單元格。 dequeueReusableCellWithIdentifier只檢索已經創建的單元格,不創建新的單元格。

這裏是如何做到這一點:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath 
    static NSString *CellIdentifier = @"HistoryItem"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    //if cell is not nil, it means it was already created and correctly dequeued. 
    if (cell == nil) { 
     //create, via alloc init, your cell here 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    return cell; 
} 
+0

我在HistoryViewController.m被放置這個,這是正確的位置?當有人點擊歷史記錄按鈕(位於主視圖中)時,我的歷史視圖就會顯示出來。 – Jonnybellman

+0

另外,我用上面的代碼,我有各種錯誤。 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@「HistoryItem」];如果沒有錯誤,我必須編輯爲: //如果單元格不爲零,則表示它已經創建並正確出列。如果(cell == nil) {//通過alloc init創建您的單元格在這裏 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@「HistoryItem」]; } return cell; } – Jonnybellman

+0

我發現另一個SO鏈接解決了我的問題。在創建新問題之前,我應該看起來更難一些http://stackoverflow.com/questions/7910206/how-can-i-create-an-uitableview-on-xcode-4-2-for-ios-5 – Jonnybellman

相關問題