Ios 以编程方式添加UITableView会在单元格左侧留下较大的空间
因此,我以编程方式将Ios 以编程方式添加UITableView会在单元格左侧留下较大的空间,ios,swift,uitableview,Ios,Swift,Uitableview,因此,我以编程方式将UITableView添加到UIViewController中,得到以下结果: 我在viewDidLoad中创建表格,如下所示: let size = view.bounds.size table = UITableView(frame: CGRectMake(0, 65, size.width, size.height-65), style: .Plain) table.delegate = self table.dataSource =
UITableView
添加到UIViewController
中,得到以下结果:
我在viewDidLoad
中创建表格,如下所示:
let size = view.bounds.size
table = UITableView(frame: CGRectMake(0, 65, size.width, size.height-65), style: .Plain)
table.delegate = self
table.dataSource = self
table.separatorInset = UIEdgeInsetsZero
table.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
这就是我如何设置单元格的方法:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "cell")
cell.selectionStyle = .None
cell.imageView?.image = blueCheck
cell.textLabel?.text = "TEXT"
cell.separatorInset = UIEdgeInsetsZero
return cell
}
我也尝试过这样做,这是对类似问题的建议答案
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
}
我也尝试过不使用可重用的单元格
和设置单元格
框架,但没有任何效果
欢迎任何帮助或想法。谢谢
编辑:
设置table.separatorInset=UIEdgeInsetsZero
是导致空单元格
向左移动的原因,如上图所示。当这被移除时,空的单元格也有很大的空间
我还尝试以编程方式使用约束,看看这是否有什么不同。很遗憾,我也得到了同样的结果。这就是我所尝试的:
let bottomConstraint = NSLayoutConstraint(item: table, attribute: NSLayoutAttribute.BottomMargin, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: table, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.TopMargin, multiplier: 1, constant: 65)
let leftConstraint = NSLayoutConstraint(item: table, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item: table, attribute: NSLayoutAttribute.TrailingMargin, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.TrailingMargin, multiplier: 1, constant: 0)
因此,iOS 9的问题是,有时您还需要设置cellLayoutMarginsFollowReadableWidth=false
,以便自定义插图和页边距。我在使用story board+约束时没有遇到过这种情况,所以我不确定这是否只是在以编程方式制作所有内容时出现的问题
以下是我为使其工作而添加的代码:
override func viewWillAppear(animated: Bool) {
if #available(iOS 9.0, *) {
table.cellLayoutMarginsFollowReadableWidth = false
} else {
// Fallback on earlier versions
}
}
override func viewDidLayoutSubviews() {
table.separatorInset = UIEdgeInsetsZero
table.layoutMargins = UIEdgeInsetsZero
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.separatorInset = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
cell.layoutMargins = UIEdgeInsetsZero
}
在将此Tableview添加为子视图之前,请检查Superview的框架。是否添加了任何约束?在以编程方式添加约束时签出此答案:只需尝试,结果相同:(控制台中关于冲突约束等的任何输出?您可以编辑您的帖子并添加您添加的约束吗?没有冲突约束。已将约束添加到帖子中。您是否在初始化约束后向视图中添加了约束?