I'm trying to keep text centred in a UITableViewCell when I have the accessoryView set to UITableViewCellAccessoryCheckmark. I'm also using a storyboard with this class.
Here's a screenshot of what I don't want.
How do I stop the text from being indented to the left?
My willDisplayCell method..
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [self cellToCheckmark]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
NSLog(@"Not Being Checked");
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.textAlignment = UITextAlignmentCenter;
if (cell.shouldIndentWhileEditing == YES) {
cell.shouldIndentWhileEditing = NO;
}
}
CellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.textAlignment = UITextAlignmentCenter;
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Google";
break;
case 1:
cell.textLabel.text = @"Bing";
break;
case 2:
cell.textLabel.text = @"Yahoo!";
break;
default:
break;
}
}
if (indexPath.row == [self cellToCheckmark]) {
NSLog(@"Being Checked");
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
NSLog(@"Not Being Checked");
cell.accessoryType = UITableViewCellAccessoryNone;
}
if (cell.shouldIndentWhileEditing == YES) {
cell.shouldIndentWhileEditing = NO;
}
return cell;
}
There are 2 things you need to do:
First, you need to make sure you set the table style to Default: UITableViewCellStyleDefault
. All other styles use a detailTextLabel
in one way or another and you won't be able to set the textLabel's alignment property.
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
Then you can set the alignment of your cell's textLabel:
cell.textLabel.textAlignment = NSTextAlignmentCenter;
Then setting the accessory to checkmark based on whatever your data requires.
cell.accessoryType = ( myDataMatches ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone );
iOS 8+ solution:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = ...;
if (/* your condition */) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.layoutMargins = UIEdgeInsetsMake(0, 40, 0, 10);
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10);
}
return cell;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With