【发布时间】:2016-09-22 09:54:26
【问题描述】:
我正在自动调整 tableview 的大小,例如 tableview 的高度会根据其内容而变化。为此,我正在使用此代码
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
dispatch_async(dispatch_get_main_queue(),{
self.tblReceipt.sizeToFit()
var frame = self.tblReceipt.frame
frame.size.height = self.tblReceipt.contentSize.height
self.tblReceipt.frame = frame
}
我能够做到这一点。
比我在表格视图中添加按钮底部。 从 tableview 设置顶部位置我正在使用此代码
let pinTop = NSLayoutConstraint(item: self.btnPrint, attribute: .Top, relatedBy: .Equal,
toItem: self.tblReceipt, attribute: .Bottom, multiplier: 1.0, constant: 10)
self.btnPrint.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activateConstraints([pinTop])
我这样做是因为那里有任何大小的 tableview。button 应该总是在 tableview 的顶部。
但这不起作用。以下是错误截图
UITableView 委托方法
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrReceipt.count
}
数据源方法
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:PrintCell = (tblReceipt.dequeueReusableCellWithIdentifier("PrintCell"))! as! PrintCell
cell.lblProductName.text = arrReceipt[indexPath.row].objectForKey("product_name") as? String
cell.lblProductPrice.text = String(format:"%.2f", arrReceipt[indexPath.row]["product_price"]!!.doubleValue)
return cell
}
【问题讨论】:
-
是的,我想要这个
-
@pedrouan 请检查我更新的问题。我在我的应用程序中添加我想要的图像。
-
@KrutarthPatel 表格高度在添加新单元格时不会改变,因此您的按钮保持在同一位置。表格高度保持不变,并且随着行数的增加,将添加更多行。
-
@raki 是的,这就是我的观点。任何解决方案?
-
@KrutarthPatel 看看 Umair Afzal 在这个帖子中的回答
标签: ios swift uitableview autolayout nslayoutconstraint