当附件是UITableViewCellAccessoryCheckmark时,如何在UITableViewCell中居中文本

W D*_*son 5 xcode uitableview ios5

当我将accessoryView设置为UITableViewCellAccessoryCheckmark时,我正在尝试将文本保持在UITableViewCell中心.我也在使用这个类的故事板.

这是我不想要的截图.

如何阻止文本向左缩进?

在此输入图像描述

我的willDisplayCell方法..

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

的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;
}
Run Code Online (Sandbox Code Playgroud)

Wri*_*sCS 9

您需要做两件事:

首先,您需要确保将表格样式设置为Default : UITableViewCellStyleDefault. 所有其他样式都detailTextLabel以某种方式使用,您将无法设置textLabel的alignment属性.

[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier]
Run Code Online (Sandbox Code Playgroud)

然后,您可以设置单元格textLabel的对齐方式:

cell.textLabel.textAlignment = NSTextAlignmentCenter;
Run Code Online (Sandbox Code Playgroud)

然后根据您的数据需要将附件设置为复选标记.

cell.accessoryType = ( myDataMatches ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone );
Run Code Online (Sandbox Code Playgroud)

截图

在此输入图像描述


Gle*_*sov 6

iOS 8+解决方案:

- (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;
}
Run Code Online (Sandbox Code Playgroud)