开发者

UITableViewCell - Adding subview while using an UITableViewCellStyle

So I have an UITableView and all of its cells are being rendered with the UITableViewCellStyleValue1.

This style works great for me within the cell.indentationLevel

Now within of the cells I need to display an UISwitch instead of the detailTextLabel. I started working at cellForRowAtIndexPath and here is my code.

UISwitch *switchField = [[UISwitch alloc] init];
switchField.frame = CGRectMake(tableV开发者_运维百科iew.bounds.size.width - 194, 8, 94, 27);
switchField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
switchField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[cell.contentView addSubview:switchField];

It works great at first but whenever I rotate the device the Switch's bounds remains the same. I thought by adding an UIView to the cell.contentView it would take of aligning whenever it needed.

What am I missing and more than anything else what should I do in order to achieve what I want to?

Thanks.


You could try:

switchField.autoresizingMask = UIAutoresizingMaskFlexibleLeft | UIAutoresizingMaskFlexibleRight;

or maybe just one or the other:

switchField.autoresizingMask = UIAutoresizingMaskFlexibleLeft;

switchField.autoresizingMask = UIAutoresizingMaskFlexibleRight;

Failing that.... you could subclass the UITableViewCell, make swithField global and override layoutSubviews:

- (void)layoutSubviews {
    [super layoutSubviews];
    switchField.frame = CGRectMake(tableView.bounds.size.width - 194, 8, 94, 27);
}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜