簡體   English   中英

UiButton位置不會以編程方式更改iOS

[英]UiButton Position not change programmatically ios

我正在自動調整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。 從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
    }

您需要將按鈕放在UITableView的最后一個單元格中,以便可以像下面那樣創建自定義單元格。

轉到文件->新建->可可接觸類,然后命名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

我希望這將有所幫助。 如果您發現任何困難,請告訴我。

為什么不考慮在代碼中創建整個UIView,然后將按鈕放在所需的位置。 示例:FooterView?

我為您找到了以下代碼:

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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM