Iphone 在UITableViewCell中对齐标签
我使用UITableView和使用UITableViewCellStyleSubtitle创建的单元格。每个单元格的高度都使用Iphone 在UITableViewCell中对齐标签,iphone,cocoa-touch,uitableview,Iphone,Cocoa Touch,Uitableview,我使用UITableView和使用UITableViewCellStyleSubtitle创建的单元格。每个单元格的高度都使用 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 委托方法 你可以在图片上看到的问题 (注:图像更新) 如何设置textLabel和detailTextLabel与单元格顶部对齐?(我真的不想通过子类化UITableViewCe
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
委托方法
你可以在图片上看到的问题
(注:图像更新)
如何设置textLabel和detailTextLabel与单元格顶部对齐?(我真的不想通过子类化UITableViewCell和重写layoutSubviews来实现这一点)
Thanx无论您如何调整单元格大小,都应该使用NSString的各种大小调整方法。这样,你就可以准确地确定单元格的高度,避免出现空白。好吧,这一次花了我一个下午的时间,但我想我算出来了。据我所知,这似乎是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;
}
然后,子类UITableViewCell并覆盖布局子视图:
#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
如果事实证明textLabel和detailTextLabel是使用自动调整大小掩码进行布局的,那么您可以在返回单元格时执行此操作:
cell.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
cell.detailTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
如果这是可行的,那么它比对单元格进行子类化要容易一些。不过我还没有尝试过。这里是另一个没有对单元格进行子类化的解决方案,因此它肯定适用于不同的表样式。(我没有检查其他解决方案。)标题和详细信息字符串在我的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;
}
看起来你的字幕文本从单元格底部流到下一个单元格,这就是我看到的吗?另外,这是在设备上发生的还是仅仅在模拟器上发生的?你能发布你的HeightForRowatineXpath实现吗?屏幕截图的触感很好。图像链接被破坏了。当然这很容易,但并不成功。布局子视图覆盖是正确的方法。回答很好,效果很好。触感很好:如果detailTextLabel.text==nil,则将4.0替换为12.0,以便在没有单元格的detailText时在视图中垂直居中textLabel。一个建议是:使用变量存储帧并在调用
cRectmake
时使用这些变量是非常值得的。在这个小方法中,您将节省14个冗余方法调用。这些冗余的方法调用可以总结并对性能产生影响。