2016-04-22 70 views
0

我使用的tableview iOS中9,斯威夫特2 我不明白为什么,当我创建一个简单的单元格的表视图,我得到的内容查看的错误的宽度,即使我设置容器查看为1000. 单元格宽度恰好为1000,但textLabel内部小于1000.分隔符居中且宽度错误。UITableViewCell的错误内容宽度

我怎样才能正确地显示我的电池,并覆盖了整个容器的看法?

所有内容均以编程方式创建。

这里是我的代码:

if tableView == nil { 
     tableView = UITableView(frame: CGRect(x: 0, y: 44/*88*/, width: self.view.bounds.size.width, height: self.view.bounds.size.height - 44/*88*/), style: UITableViewStyle.Plain) 
     tableView!.delegate = self 
     tableView!.backgroundColor = .clearColor() 
     tableView!.dataSource = self 
     tableView!.rowHeight = 40.0 
     tableView!.allowsSelection = false 
     tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 

     view.addSubview(tableView!) 

     tableView!.reloadData() 
    } 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    var MyIdentifier: String = "MyReuseIdentifier" 
    var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(MyIdentifier) 
    if cell == nil { 
     cell = UITableViewCell(style: .Default, reuseIdentifier: MyIdentifier) 
    } 
    cell!.textLabel!.text = "Test" 
    cell!.backgroundColor = .clearColor() 
    cell!.textLabel!.textColor = .blackColor() 
    return cell! 
} 
+0

你的容器视图主视图控制器'视图'? –

+0

尝试更新“textLabel”的框架值。 – BobGao

+0

这里是层次: - 的viewController(不是主要的,但另一个)有自己的看法 - 的tableView即视图 - 控制视图的子视图。 – yax

回答

0

尽管小区的宽度是整个视图的宽度,它的内容画面的(我认为)8点在各方面的插图,因此是很有意义的标签不被在整个屏幕上

为了有一个标签,去在屏幕的整个宽度创建一个自定义单元格级,并添加约束其标签考虑到这一点。 (标签领先的内容观统领= -8.0)

0

您可以检查

tableView.contentInset 

,然后更新左插图。另外,检查你没有给出任何约束,比如leadingSpace。或UITableView本身的边缘。同样,你有

cell.separatorInset 

默认有剩下的5填充,所以使这个0

+0

都尝试,没有工作..我看到细胞,现在到达细胞末端之间唯一的分隔符...但为textLabel仍然“中心”,而不是从x = 0 – yax

+1

开始,然后尝试设置layoutMargins,tableView.cellLayoutMarginsFollowReadableWidth = NO;如果这是特定于ios 9。 – user3300864

2
tableView!.cellLayoutMarginsFollowReadableWidth = false 

这是解决方案!

这是没有必要设置contentInsets。