【问题标题】:How to create UITableView like GRID in landscape mode?如何在横向模式下创建类似 GRID 的 UITableView?
【发布时间】:2011-10-10 09:25:57
【问题描述】:

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

#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;
}

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

【问题讨论】:

    标签: uitableview iphone-sdk-4.3


    【解决方案1】:

    这将需要比您提供的方法更多的代码。 我的建议是您可以为每个字段创建一个 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   
        }
    }
    

    希望这会有所帮助。

    【讨论】:

    • 我会尽力为您投票并接受解决方案。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    相关资源
    最近更新 更多