2013-11-14 131 views
0

我正在嘗試做我認爲很簡單的事情,但看起來相當複雜。在ViewDidAppear中填充TableView

我想創建一個排行榜屏幕。

我有以下幾點:

NSArray* playerNames 
NSArray* playerScores 

我的排行榜標籤是一個視圖控制器。裏面,它有一個桌面視圖。 tableview有一個插座。

#import <UIKit/UIKit.h> 
#import "AppDelegate.h" 

@interface LeaderboardViewController : UIViewController 
{ 

} 
- (void)viewDidAppear:(BOOL)animated; 
- (void) viewWillDisappear:(BOOL)animated; 
@property (weak, nonatomic) IBOutlet UITableView *leaderboardTable; 
- (SimonGameModel*) model; 
@end 

當視圖沒有加載時,我從我的模型中獲得上面的兩個數組(兩個長度相同)。它們對應於最新的分數。

我需要的是爲每個表單元格TAVE 2個標貼,讓我結束了一個排行榜,看起來是這樣的:

Tim   200 
John   100 
Jack   50 

等等

我一直在閱讀蘋果的docs將近一個小時,我很困惑如何做到這一點。

我用我想要的標籤創建了一個原型。

謝謝

回答

1
-(void)viewDidLoad { 
    [leaderboardTable setDataSource:self]; 
    [leaderboardTable setDelegate:self]; 
} 

你必須用這種方式創建自定義單元格:

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

return [playerNames count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


    static NSString *CellIdentifier = @"leader"; 
    UITableViewCell *cell = [leaderboardTable dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

UILabel *labelName = [[UILabel alloc] initWhitFrame:CGRectMake(5, 0,160,44); 
[labelName setTextAlignment:NSTextAlignmentLeft]; 
labelName.textColor = [UIColor blackColor]; 
[cell.contentView addSubView:labelName]; 

UILabel *labelValue = [[UILabel alloc] initWhitFrame:CGRectMake(165, 0, 150, 44); 
[labelValue setTextAlignment:NSTextAlignmentRight]; 
labelValue.textColor = [UIColor blackColor]; 
[cell.contentView addSubView:labelValue]; 

    } 

labelName.text = [playerNames objectAtIndex:indexPath.row]; 
labelValue.text = [playerScores objectAtIndex:indexPath.row]; 

return cell; 
} 
0

這聽起來像你沒有將你的LeaderboardViewController設置爲你的tableview的dataSource。 LeaderboardViewController必須符合UITableViewDataSource協議:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006941

另外,記得要註冊一個UITableViewCell廈門國際銀行或類與實現代碼如下。

你會被填充你的細胞與您的陣列數據- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

+0

你能不能給我一份有存根方法,一個簡單的例子?我需要另一個繼承UITableView的類嗎? 我的原型有兩個標籤,這是如何起作用的。我也不確定筆尖部分。 謝謝 – jmasterx