添加附件类型会在UITableViewCell中移动Label

时间:2016-03-01 02:08:41

标签: ios swift uitableview

我正在尝试制作类似单选按钮的东西;但是,我正在使用UITableView。我有以下代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .Default, reuseIdentifier: "sortcell")
    if indexPath.row == 0 {
        cell.textLabel?.text = "Student"
    } else {
        cell.textLabel?.text = "Teacher"
    }
    cell.textLabel?.textAlignment = .Center
    cell.textLabel?.font = getFont(17.5)
    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

var selectedIndexPath = 10

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selected = true

    if indexPath.row == 0 {
        sort = "Student"
        let cell = sortTable.cellForRowAtIndexPath(indexPath)
        cell?.accessoryType = .Checkmark
        let otherIndexPath = NSIndexPath(forRow: 1, inSection: 0)
        let otherCell = sortTable.cellForRowAtIndexPath(otherIndexPath)
        otherCell?.accessoryType = .None
    } else {
        sort = "Teacher"
        let cell = sortTable.cellForRowAtIndexPath(indexPath)
        cell?.accessoryType = .Checkmark
        let otherIndexPath = NSIndexPath(forRow: 0, inSection: 0)
        let otherCell = sortTable.cellForRowAtIndexPath(otherIndexPath)
        otherCell?.accessoryType = .None
    }

    sortTable.deselectRowAtIndexPath(indexPath, animated: true)

}

这允许用户在选择他或她是学生还是教师之间切换。但是,添加accessoryType后,文本标签会略微移动,如下所示:

默认: Default

点击时: When tapped

我该如何解决这个问题?我希望文本标签保持在同一个地方,而不是向左移动。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

尝试将<?php $ids = array(); $pages = get_pages("child_of=".$post->ID); if ($pages){ foreach ($pages as $page){ $ids[] = $page->ID; } } $paged = (get_query_var("paged")) ? get_query_var("paged") : 1; $args = array( "paged" => $paged, "post__in" => $ids, "posts_per_page" => 5, "post_type" => "page" ); query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); ?> <div> <h3><?php the_title(); ?></h3> <div><?php the_date(); ?></div> <p><?php the_content(); ?></p> </div> <?php endwhile; ?> <?php endif; paging_nav(); wp_reset_query(); ?> 设为textLabel.textAlignment

答案 1 :(得分:0)

我认为这是因为Check Mark将视图推向左侧。您可能需要自定义单元格的视图