标签在UITableViewCell中对齐

Igo*_*gor 20 iphone cocoa-touch uitableview

我使用的UITableView 使用创建的细胞UITableViewCellStyleSubtitle.每个单元格的高度都是使用动态调整的

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

委托方法.

你可以在图片上看到的问题

(注意:图片已更新)

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

如何将textLabel和detailTextLabel的对齐设置到单元格的顶部?(我真的不会通过继承UITableViewCell并重写layoutSubviews来做到这一点)

感谢名单

Chr*_*ett 36

嗯,这个花了我一个下午,但我想我想通了.据我所知,这似乎是UITableViewCell如何布局textLabel和detailTextLabel的一个错误.当您设置行高时,它似乎为两个标签分配相同的高度,这意味着您获得了上面看到的行为,即使detailTextLabel需要更多空间.以下是我为解决问题所做的两件事.我不得不将UITableViewCell子类化来修复它,但它只需要很少量的代码.

首先,确保正确计算每行的高度.将此方法放入表视图委托中.用您自己的字体方法替换:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSString *cellDetailText = [[self itemForIndexPath: indexPath] detailDescription];
   NSString *cellText = [[self itemForIndexPath: indexPath] description];
   // The width subtracted from the tableView frame depends on:
   // 40.0 for detail accessory
   // Width of icon image
   // Editing width
   // I don't think you can count on the cell being properly laid out here, so you've
   // got to hard code it based on the state of the table.
   CGSize constraintSize = CGSizeMake(tableView.frame.size.width - 40.0 - 50.0, CGFLOAT_MAX);
   CGSize labelSize = [cellText sizeWithFont: [self cellTextLabelFont] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
   CGSize detailSize = [cellDetailText sizeWithFont: [self cellDetailTextLabelFont] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

   CGFloat result = MAX(44.0, labelSize.height + detailSize.height + 12.0); 
   return result;
}
Run Code Online (Sandbox Code Playgroud)

然后,子类UITableViewCell并覆盖layoutSubviews:

#import "UITableViewCellFixed.h"


@implementation UITableViewCellFixed
- (void) layoutSubviews {
   [super layoutSubviews];
   self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x, 
                                      4.0, 
                                      self.textLabel.frame.size.width, 
                                      self.textLabel.frame.size.height);
   self.detailTextLabel.frame = CGRectMake(self.detailTextLabel.frame.origin.x, 
                                           8.0 + self.textLabel.frame.size.height, 
                                           self.detailTextLabel.frame.size.width, 
                                           self.detailTextLabel.frame.size.height);
}
@end
Run Code Online (Sandbox Code Playgroud)

  • 一个建议:使用变量存储帧并在调用`CGRectMake`时使用它们是非常值得的.您将使用这种小方法保存14个冗余方法调用.这些冗余方法调用可以总结并对性能产生影响. (2认同)

SK9*_*SK9 8

这是另一个没有子类化单元格的解决方案,所以它肯定适用于不同的表格样式.(我还没有检查过其他解决方案.)标题和详细信息字符串在我的UITableViewController标头中声明并已定义.它们不是很长,当然在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;

}
Run Code Online (Sandbox Code Playgroud)