0

我想创建uitableview这样的图像。从服务器加载数据并将值分配给 Row 的列。我看到了堆栈的链接,但对我没有帮助。 更新 我的代码: -

#pragma mark UITableViewDelegate methods

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleGray;      
    // Configure the cell...
    RankModel *model = [modelArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@    %@     %@     %@     %@     %@",  model.level, model.name, model.score, model.rightAnswersCount, model.currentRank, model.country];
    return cell;
}

但我想像给定的图像一样显示。所以请帮助我克服这个问题。提前致谢。

4

1 回答 1

2

好吧,这将需要比您提供的方法更多的代码。我的建议是您可以为每个字段创建一个 UILabel,使用单个 NSString。不要使用 cell.textLabel,而是在 cell.contentView 上添加您的内容,然后您可以管理每个标签的颜色、背景颜色和标签的大小。“网格”外观可以通过将白色分配给 contentView 并将绿色分配给每个标签的背景来呈现。例如,在您的单元格创建后:

cell.contentView.backgroundColor = [UIColor clearColor];

UILabel* aLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0.0, 100, 44)];
aLabel.tag = indexPath.row;
aLabel.textAlignment = UITextAlignmentLeft;
aLabel.textColor = [UIColor whiteColor];
aLabel.text = @"Test";
aLabel.backgroundColor = [UIColor greenColor];
[cell.contentView addSubview:aLabel];
[aLabel release];

从 201 或更多开始您的下一个标签,以留下白色垂直线的印象。将行索引保留在标签中,以便您可以在以下位置管理备用颜色:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell
                                                        *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0 || indexPath.row%2 == 0) {
        // use light green, get access to the labels via [cell.contentView viewWithTag:indexPath.row]
    } else {
        // use dark green   
    }
}

希望这可以帮助。

于 2011-10-10T14:43:37.000 回答