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.
精彩评论