iphone - UITableViewCell 中的标签对齐

标签 iphone cocoa-touch uitableview

我将UITableView与使用UITableViewCellStyleSubtitle创建的单元格一起使用。使用

动态调整每个单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

委托(delegate)方法。

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

(注意:图像已更新)

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

如何设置textLabel和detailTextLabel与单元格顶部对齐? (我真的不想通过子类化 UITableViewCell 并覆盖 layoutSubviews 来做到这一点)

谢谢

最佳答案

嗯,这个问题花了我一个下午的时间,但我想我已经弄清楚了。据我所知,这似乎是 UITableViewCell 布局textLabel 和detailTextLabel 的方式中的一个错误。当您设置行高时,它似乎为两个标签分配了相等的高度,这意味着您将得到与上面看到的完全相同的行为,即使detailTextLabel需要更多空间。这是我为解决该问题所做的两件事。我必须子类化 UITableViewCell 来修复它,但这是最少量的代码。

首先,确保正确计算每行的高度。将此方法放入您的 TableView 委托(delegate)中。将字体方法替换为您自己的:

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

然后,子类化 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

关于iphone - UITableViewCell 中的标签对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1132029/

相关文章:

iphone - Xcode 4 中的 "Read/Write class files"在哪里?

ios - 这个 View 可能没有收到 initWithFrame : or initWithCoder:

objective-c - NSTimer 触发 UIProgressView 失败

iPhone:如何更改 UITableViewCell 中默认删除按钮的框架

uitableview - 如何使用按钮标签快速访问自定义单元格的内容?

ios - 从代码中调用 IBAction 的最佳方法是什么?

iphone - 优化 OpenGL ES 应用程序。我应该尽可能避免调用 glVertexPointer 吗?

iphone - applicationWillTerminate 在崩溃时被调用

objective-c - UI 滚动时应用程序停止从套接字接收数据

iphone - 我可以访问 iOS 邮件应用程序数据吗?