当附件是UITableViewCellAccessoryCheckmark时,如何将文本置于UITableViewCell中

时间:2012-05-31 21:43:22

标签: xcode ios5 uitableview

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

这是我不想要的截图。

如何阻止文字向左缩进?

enter image description here

我的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;
    }
}

的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;
}

5 个答案:

答案 0 :(得分:9)

您需要做两件事:

首先,您需要确保将表格样式设置为默认值:UITableViewCellStyleDefault。所有其他样式以某种方式使用detailTextLabel,您将无法设置textLabel的对齐属性。

[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier]

然后您可以设置单元格textLabel:

的对齐方式
cell.textLabel.textAlignment = NSTextAlignmentCenter;

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

cell.accessoryType = ( myDataMatches ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone );

截图

enter image description here

答案 1 :(得分: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;
}

答案 2 :(得分:1)

这些解决方案适用于默认单元格。如果您有自己的自定义单元格,最简单的方法是使标签对左边距和右边距都有约束,然后为左边距约束创建一个出口,并在{{1}中设置其常量方法:

cellForRowAtIndexPath

这样,为accessoryType添加的右边距也会添加到左侧。

答案 3 :(得分:0)

不知道问题是否解决

if (/* your condition */) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.indentationLevel = 4;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.indentationLevel = 0;
    }

答案 4 :(得分:0)

似乎有一个更简单的答案(见this answer)。简而言之,您将标签置于单元格的内容视图中。相反,将它放在细胞本身中,然后它就不会移动。

试了一下,它适用于iOS 11.2。

enter image description here