自定义UITableViewCell子视图布局 [英] Custom UITableViewCell subview layout
问题描述
我创建了一个自定义 UITableViewCell
,风格类似于 UITableViewCellStyleValue1
,但在右侧而不是 detailTextLabel
。我有点困惑,如何正确调整编辑字段的大小。我想要考虑 textLabel
的长度,以及 contentView
的大小,并且能够
I'm creating a custom UITableViewCell
, similar in style to UITableViewCellStyleValue1
but with an editable text field on the right side instead of the detailTextLabel
. I'm a little confused on how to size the edit field properly. I want to take the length of the textLabel
into account, as well as the size of the contentView
, and be able to do the Right Thing when its table view is in edit mode, or if an accessory view (like a disclosure arrow) is added.
但是
我想我可以在tableView的委托 -tableView:willDisplayCell:forRowAtIndexPath:
方法中以自定义方式处理这完全违反了具有可重用单元格类的目的。
I figure I can handle this in a custom fashion in the tableView's delegate -tableView:willDisplayCell:forRowAtIndexPath:
method, but that completely defeats the purpose of having a reusable cell class.
我必须在这里缺少一些关键的概念,但是我失去了。任何人?
I must be missing some key concept here, but I'm at a loss. Anyone?
谢谢!
randy
Thanks! randy
推荐答案
你可能想在UITableViewCell子类中控制你的布局。例如:
You may want to take control of your layout in your UITableViewCell subclass. For example:
- (void)layoutSubviews
{
CGRect b = [self bounds];
b.size.width += 30; // allow extra width to slide for editing
b.origin.x -= (self.editing) ? 0 : 30; // start 30px left unless editing
[contentView setFrame:b];
// then calculate (NSString sizeWithFont) and addSubView, the textField as appropriate...
//
[super layoutSubviews];
}
这篇关于自定义UITableViewCell子视图布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!