2016-06-15 97 views
0

我需要在UITableView中顯示我的數組,但我無法正確執行此操作。它不顯示標籤文本,標籤爲空白。這是我迄今爲止所做的。只有圖像顯示在我的UITableView如何填充UITableView?

NSMutableArray *opp; 
NSMutableArray *prop; 
NSMutableArray *checkA = [NSMutableArray array]; 
NSArray *values = [dict allValues]; 
NSArray *keys = [dict allKeys]; 

NSArray *games = [values valueForKey:@"Game"]; 
count = [[games objectAtIndex:0]count]; 
NSUInteger index = 0; 
while (index < count) { 
    Opponent = [[[[[games objectAtIndex:0] valueForKey:@"Opponent"]objectAtIndex:index]valueForKey:@"OpponentName"]valueForKey:@"text"]; 
    [opp addObject:Opponent]; 

    Proponent = [[[[[games objectAtIndex:0] valueForKey:@"Proponent"]objectAtIndex:index]valueForKey:@"ProponentName"]valueForKey:@"text"]; 
    [prop addObject:Proponent]; 
index++ 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return count; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section 
{ 
switch(section) 
{ 
    case 0: return 2; // section 0 has 2 rows 
    case 1: return 1; // section 1 has 1 row 
    default: return 0; 
}; 
} 

// Return the row for the corresponding section and row 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells" forIndexPath:indexPath]; 

UIImageView *images = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 50, 50)]; 
[images setImage:[UIImage imageNamed:[img objectAtIndex:indexPath.row]]]; 
[cell addSubview:images]; 

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(131, 27, 59, 21)]; 
label1.text = [opp objectAtIndex:indexPath.row]; 
[cell addSubview:label1]; 
NSLog(@"label 1: ",label1.text); 
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(231, 27, 67, 21)]; 
label2.text = [prop objectAtIndex:indexPath.row]; 
[cell addSubview:label2]; 
NSLog(@"label 2: ",label2.text); 

return cell; 
} 

現在它正在工作,忘記初始化我的字符串。現在的問題是它只顯示3行。我期待29行,因爲我的opp有29個對象

+0

通過靜態行值試試這個代碼[cell.contentView addSubview:LABEL1]。你改變你的標籤位置 – iOS

+0

現在它正在工作,但它只顯示3行。我期待29行導致我的opp有29個對象 – drbj

回答

0

我不認爲你需要部分,因爲你沒有在任何地方與他們打交道,所以刪除numberOfSectionsInTableView方法並返回numberOfRowsInSection方法中的'計數'。通過部分的默認數量爲1。請參閱下面的代碼,

NSMutableArray *opp; 
NSMutableArray *prop; 
NSMutableArray *checkA = [NSMutableArray array]; 
NSArray *values = [dict allValues]; 
NSArray *keys = [dict allKeys]; 

NSArray *games = [values valueForKey:@"Game"]; 
count = [[games objectAtIndex:0]count]; 
NSUInteger index = 0; 
while (index < count) { 
    Opponent = [[[[[games objectAtIndex:0] valueForKey:@"Opponent"]objectAtIndex:index]valueForKey:@"OpponentName"]valueForKey:@"text"]; 
    [opp addObject:Opponent]; 

    Proponent = [[[[[games objectAtIndex:0] valueForKey:@"Proponent"]objectAtIndex:index]valueForKey:@"ProponentName"]valueForKey:@"text"]; 
    [prop addObject:Proponent]; 
index++ 
} 

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

// Return the row for the corresponding section and row 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells" forIndexPath:indexPath]; 

UIImageView *images = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, 50, 50)]; 
[images setImage:[UIImage imageNamed:[img objectAtIndex:indexPath.row]]]; 
[cell addSubview:images]; 

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(131, 27, 59, 21)]; 
label1.text = [opp objectAtIndex:indexPath.row]; 
[cell addSubview:label1]; 
NSLog(@"label 1: ",label1.text); 
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(231, 27, 67, 21)]; 
label2.text = [prop objectAtIndex:indexPath.row]; 
[cell addSubview:label2]; 
NSLog(@"label 2: ",label2.text); 

return cell; 
} 
0

試試這個代碼,因爲你是在表

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