Iphone 在UITableViewCell中更改textLabel和detailTextLabel之间的间距?

Iphone 在UITableViewCell中更改textLabel和detailTextLabel之间的间距?,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,是否有一种方法可以更改UITableViewCell中textLabel和detailTextLabel之间的间距?(无子类UITableViewCell)创建的自定义子类,并实现执行此操作的方法: - (void) layoutSubviews { [super layoutSubviews]; //my custom repositioning here } 如果不想子类化,可以通过方法swizzling来实现,但总的来说,这是个坏主意™. 这可以在不使用NSAttributedS

是否有一种方法可以更改UITableViewCell中textLabel和detailTextLabel之间的间距?(无子类UITableViewCell)

创建的自定义子类,并实现执行此操作的方法:

- (void) layoutSubviews {
  [super layoutSubviews];
  //my custom repositioning here
}

如果不想子类化,可以通过方法swizzling来实现,但总的来说,这是个坏主意™.

这可以在不使用NSAttributedString和attributedText属性进行子类化的情况下完成,如下所示:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyReuseIdentifier"];

NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"Some Text"];
cell.textLabel.attributedText = text;

NSMutableParagraphStyle *subtitleParagraphStyle = [NSMutableParagraphStyle new];
subtitleParagraphStyle.minimumLineHeight = 20;

NSMutableAttributedString *subText = [[[NSAttributedString alloc] initWithString:@"Some Subtitle Text"] mutableCopy];
[subText addAttribute:NSParagraphStyleAttributeName value:subtitleParagraphStyle range:NSMakeRange(0, subText.length)];

cell.detailTextLabel.attributedText = subText;
您所做的是强制字幕的行高大于正常值。利用文本和子文本的线条高度可以帮助您实现所需的目标。应与iOS 7+兼容


几年太晚了,但希望有人发现它有用。

没有子类化是不可能的吗?@iPhone Developer-是的,没有子类化是可能的,但它需要潜入运行时并手动切换方法和内容。完全可行,也没有那么困难,但它使您的实现更加脆弱。另外,它会影响应用程序中的每个
UITableViewCell
,而不是只影响几个。@iPhone开发者:如果您需要更改默认的UITableViewCell,子类化总是最好的方法@戴夫:很好的解释。