【问题标题】:UiButton Position not change programmatically iosUiButton位置不会以编程方式更改ios
【发布时间】: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


【解决方案1】:

您需要将按钮放在UITableView 的最后一个单元格中,这样您就可以创建一个如下所示的自定义单元格。

转到 File->New->Cocoa Touch Class 然后命名 yourCell 并创建 Xib

现在单击新创建的 Xib 并设计您的单元格。在您的情况下,您可以在单元格中放置一个按钮并应用一些 autoLayout 约束,如下所示

现在在自定义单元格类中创建按钮的 IBOutlet。你自定义的单元格类应该看起来像这样。

import UIKit

class YourTableViewCell: UITableViewCell {

@IBOutlet weak var yourButton: UIButton!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}


class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourTableViewCell {

    let kYourTableViewCellIdentifier = "kYourTableViewCellIdentifier"
    tableView.registerNib(UINib(nibName: "YourTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourTableViewCellIdentifier)
    
    let cell = tableView.dequeueReusableCellWithIdentifier(kYourTableViewCellIdentifier, forIndexPath: indexPath) as! YourTableViewCell
    
    return cell
}

}

您的自定义单元格现在可以使用了。所以在你的 tableViewController 类cellForRowAtIndexPath 中写下这段代码

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

if (indexPath.row == (arrReceipt.count)) {
    
    let cell = YourTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
    // code for last cell 
    return cell

} else {
    let cell:PrintCell = (tblReceipt.dequeueReusableCellWithIdentifier("ButtonCell"))! as! PrintCell

    // code for other rows in tableView
    return cell
   }    
}

numberOfRowsInSection 中返回 yourDataSourceArray.count + 1

我希望这会有所帮助。如果您发现任何困难,请告诉我。

【讨论】:

  • 警告在返回 UITableViewCell() 将永远不会执行
  • opps,抱歉删除那行
  • @UmairAfzal 我试过你的代码。但我在 tableview 中看不到按钮
  • @pedrouan 感谢兄弟的努力和善意
  • @KrutarthPatel 你面临什么问题?调试并告诉我一些具体问题。
【解决方案2】:

为什么不考虑在代码中创建一个完整的 UIView,然后将按钮放置在您需要的位置。示例:页脚视图?

我为你找到了这段代码:

CGRect frame = CGRectMake(0,0,320,40);
UIView *footerView = [[UIView alloc] initWithFrame:frame];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton addTarget:self action:@selector(btnAddRowTapped:)     forControlEvents:UIControlEventTouchUpInside];
aButton.frame = frame;
[footerView addSubView:aButton];
[yourTableNmae setTableFooterView:footerView];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2019-05-10
    • 2015-10-15
    相关资源
    最近更新 更多