标签在UITableViewCell中对齐 [英] Labels aligning in UITableViewCell

查看:130
本文介绍了标签在UITableViewCell中对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 UITableView 和使用 UITableViewCellStyleSubtitle 创建的单元格。使用

I use UITableView with cells created using UITableViewCellStyleSubtitle. Every cell's height is dynamically adjusted using the

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

委托方法。

问题你可以看到图片

注意:图片更新

http://img.skitch.com/20090715-g7srxgee2d7fhi5rab2wufrdgm.png

如何将textLabel 和detailTextLabel 的对齐设置到单元格的顶部? (我真的不会通过继承UITableViewCell并覆盖layoutSubviews来实现它)

How to set up align of textLabel and detailTextLabel to the top of the cell? (I really don't wont to do it by subclassing UITableViewCell and overriding layoutSubviews)

Thanx

推荐答案

这是另一种没有子类化单元格的解决方案,所以它肯定适用于不同的表格样式。 (我没有检查过其他解决方案。)标题和详细信息字符串在我的UITableViewController标头中声明并已定义。它们不是很长,当然在4000高度内!

Here is another solution without subclassing cells, so it certainly works with different table styles. (I haven't checked other solutions.) The title and detail strings are declared in my UITableViewController header and already defined. They aren''t very long, certainly well within height 4000!

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CGRect frame = [UIScreen mainScreen].bounds;
    CGFloat width = frame.size.width;

    int section = indexPath.section;
    int row = indexPath.row;

    NSString *title_string = [title_strings_array objectAtIndex:row];
    NSString *detail_string = [detail_strings_array objectAtIndex:row];

    CGSize title_size = {0, 0};
    CGSize detail_size = {0, 0};

    if (title_string && [title_string isEqualToString:@""] == NO ) {
        title_size = [title_string sizeWithFont:[UIFont systemFontOfSize:22.0]
                              constrainedToSize:CGSizeMake(width, 4000)
                                  lineBreakMode:UILineBreakModeWordWrap];
    }

    if (detail_string && [title_string isEqualToString:@""] == NO ) {
        detail_size = [detail_string sizeWithFont:[UIFont systemFontOfSize:18.0]
                                constrainedToSize:CGSizeMake(width, 4000)
                                    lineBreakMode:UILineBreakModeWordWrap];
    }

    CGFloat title_height = title_size.height;
    CGFloat detail_height = detail_size.height;

    CGFloat content_size = title_height + detail_height;

    CGFloat height;

    switch ( section ) {

        case 0:
            height = content_size;
            break;

        //Just in case  
        default:
            height = 44.0;
            break;

    }

    return height;

}

这篇关于标签在UITableViewCell中对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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