I'm trying to develop a UITableView which has sectionIndexTitles and which cells have accessory buttons.
The problem is that the scrollbar for the index titles decreases the width of the cells and so the accessory buttons are at an ugly position...
Is it possible to move the button slightly to the left that they are inside the cell?
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = @"text";
}
return cell;
}
As you can see I don't manipulate the cell or something like that.
And here you can see the result:
There is a way to move default accessoryView, but its pretty hacky. So it might stop working one day when new sdk arrives.
Use at your own risk (this code snippet moves any accessoryView 8 pixels to the left, insert it inside -(void)layoutSubviews method of desired uitableviewcell subclass):
if (self.accessoryView) {
r = self.accessoryView.frame;
r.origin.x -= 8;
self.accessoryView.frame = r;
} else {
UIView* defaultAccessoryView = nil;
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) {
defaultAccessoryView = subview;
break;
}
}
r = defaultAccessoryView.frame;
r.origin.x -= 8;
defaultAccessoryView.frame = r;
}