填充表格视图单元格

时间:2013-12-01 19:00:09

标签: ios objective-c uitableview

我目前有一个包含2个具有单独键的对象的字典,我想分别用每个对象填充表格视图单元格。所以说例如我的字典有两个对象,一个是键北,另一个是键,键是南。现在我希望表视图有2个单元格,一个包含北方,另一个包含南方。我该怎么做呢?到目前为止,我尝试了下面的代码,但只能覆盖它。

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

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    NSMutableDictionary *news = (NSMutableDictionary *)[feeds objectAtIndex:indexPath.row];
    [cell.textLabel setText:[news objectForKey:@"frstDirection"]];
    [cell.textLabel setText:[news objectForKey:@"secDirection"]];

    return cell;
}

3 个答案:

答案 0 :(得分:2)

如果您确定只有两个对象,则可以使用它:

NSMutableDictionary *news = (NSMutableDictionary *)[feeds objectAtIndex:indexPath.row];
if(indexPath.row==0){
     [cell.textLabel setText:[news objectForKey:@"frstDirection"]];
}else if(indexPath.row==1){
    [cell.textLabel setText:[news objectForKey:@"secDirection"]];
}

答案 1 :(得分:1)

if if condition,if(indexPath.row%2 == 0)然后第一个方向,否则第二个方向。通过这种方式,您将先使用第一个和第二个来交替单元格。

答案 2 :(得分:1)

在你的情况下,你试图设置两次单元格的值,所以每次你得到的值都是uitableviewcell。

请尝试按照以下代码 [cell.textLabel setText:[news objectForKey:@“frstDirection”]];

NSMutableString *teststring = [NSMutableString string];
[teststring appendString:[news objectForKey:@"frstDirection"]];
[teststring appendString:[news objectForKey:@"secDirection"]];
[cell.textLabel setText:teststring];
相关问题