如何填充UITableView?

时间:2016-06-15 02:22:00

标签: objective-c 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排,因为我的对手有29个对象

2 个答案:

答案 0 :(得分:0)

我认为您不需要部分,因为您没有在任何地方处理它们,因此请删除numberOfSectionsInTableView方法并在numberOfRowsInSection方法中返回'count'。默认情况下,部分的数量为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;
}

答案 1 :(得分:0)

尝试此代码,因为您在表

中传递静态行值
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:    (NSInteger)section
{
    return count;
}