Andrey has a good solution, but if you use an accessory, you need this code:
const int MARGIN = 16;
- (void)layoutSubviews {
[super layoutSubviews];
CGRect frame = self.imageView.frame;
frame.origin.x += MARGIN;
self.imageView.frame = frame;
frame = self.textLabel.frame;
frame.origin.x += MARGIN;
frame.size.width -= 2 * MARGIN;
self.textLabel.frame = frame;
frame = self.detailTextLabel.frame;
frame.origin.x += MARGIN;
frame.size.width -= 2 * MARGIN;
self.detailTextLabel.frame = frame;
if (self.accessoryType != UITableViewCellAccessoryNone) {
float estimatedAccesoryX = MAX(self.textLabel.frame.origin.x + self.textLabel.frame.size.width, self.detailTextLabel.frame.origin.x + self.detailTextLabel.frame.size.width);
for (UIView *subview in self.subviews) {
if (subview != self.textLabel &&
subview != self.detailTextLabel &&
subview != self.backgroundView &&
subview != self.contentView &&
subview != self.selectedBackgroundView &&
subview != self.imageView &&
subview.frame.origin.x > estimatedAccesoryX) {
frame = subview.frame;
frame.origin.x -= MARGIN;
subview.frame = frame;
break;
}
}
}
}
There is no easy way to handle the accessory. I have been looking for a while, and this is the best solution I've seen so far.
source
share