2013-10-13 23 views
0

如何將UITableView添加到現有視圖?如何將UITableView添加到現有視圖

我加入了通過界面生成器控制,並添加正確的委託給主視圖控制器(包括添加表細胞功能的.m文件)

但沒有被調用,並沒有得到填充,例如,cellForRowAtIndexPath函數永遠不會被調用。

在.m文件.h文件中

@interface GameViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

@property (weak, nonatomic) IBOutlet UITableView *friendsScoresView; 

init { 
    _friendsScoresView.delegate = self; 
    _friendsScoresView.dataSource = self; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // return number of rows 
    return 3; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyBasicCell"]; 
    //NSMutableArray *nearbyScores = gclb->nearbyScores; 

    GCLeaderboardScore *playerScore = [gclb->nearbyScores objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [NSString stringWithFormat:@"%f",playerScore->score ]; 
    cell.imageView.image = playerScore->photo; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // handle table view selection 
} 

我缺少什麼?另外我該如何告訴桌面更新/刷新其內容?

+1

您是否也設置了數據源? 要更新內容,只需編寫[tableView reloadData] –

+0

看我的更新..我相信我做了..? – Chris

+0

你做到了!你確定tableview(你在IB中添加的那個)鏈接到你的代碼? –

回答

1

如果您在IB中添加了UITableViewController,然後單擊右側的插座選項卡並連接IB中的數據源和委託。如果您想要在代碼中執行此操作,請爲您的表創建一個IBOutlet變量,並在您的變量上設置委託和數據源屬性。另外,由於NIB尚未加載,因此您無法在init上執行UIViewController。在viewDidLoad上做。

+0

嗨喬爾,謝謝你的回答。我設置了一個'IBOutlet',然後像上面那樣設置數據源和委託代碼 - 稍後更新我的問題以顯示'IBOutlet' – Chris

+0

查看更新回答 – Joel

+0

非常感謝,只是一個快速添加 - 我加載表的數據異常,並顯然返回零表格單元格不工作 - 我怎麼不顯示,直到我的數據已加載? – Chris

相關問題