如何在横向模式下创建类似 GRID 的 UITableView? [英] How to create UITableView like GRID in landscape mode?

查看:24
本文介绍了如何在横向模式下创建类似 GRID 的 UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wanna create the uitableview like this image . loading data from server and assigning the values to columns of Row. I saw the link of stack, but not helpful to me. UPDATE My code: -

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

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

but I wanna display like the given image. so please help me to overcome this problem. thanks in advance.

推荐答案

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

Well this will require a bit more code than the method you present. My suggestion is you could create a UILabel for each of your fields, instrad of using a single NSString. Don't use cell.textLabel, but rather add your content on cell.contentView and then you can manage each label's color, background color and labels' sizes. The "grid" look can be rendred by assigning a white color to contentView and assigning a green color for example to each label's background. For example, after your cell is created :

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 或更多开始您的下一个标签,以留下白色垂直线的印象.将行索引保留在标签中,以便您可以在 :

Start your next label at 201 or more to leave an impression of a white vertical line. Keep the row index in the tag so you can manage alternate colors in :

- (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   
    }
}

希望这会有所帮助.

这篇关于如何在横向模式下创建类似 GRID 的 UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆