2016-09-22 21 views
-1

我正在做tableview的自动调整tableview的高度会根据其内容而改变。为此,我正在使用此代码UiButton通过编程方式不改变位置ios

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的大小是there.button应该始终从顶部的tableview。

但这不起作用。下面是错误的屏幕截图

enter image description here

enter image description here

的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 
    } 
+0

是exactly.i想这 –

+0

@pedrouan请检查我的更新question.i添加图片我想在我的应用程序。 –

+2

@KrutarthPatel表格高度不会在添加新单元格时发生变化,因此您的按钮将保持在同一位置。表格高度保持不变,并且在增加行数时会增加额外的行。 – raki

回答

3

你需要把你的按钮中的最后一个单元格UITableView你可以像下面那样创建一个自定义Cell。

转到文件 - >新建 - >可可触摸类然后将其命名yourCell创造XIB

enter image description here

现在点击创建newaly厦门国际银行和设计细胞。你的情况,你可以把一个按钮,在细胞和一些应用的autoLayout约束如下

enter image description here

现在,在您的自定义单元格类创建按钮,一个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

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

+0

警告的UITableViewCell()将永远不会执行 –

+0

哎呀,对不起删除该行 –

+0

@UmairAfzal我想你的code.but我无法看到的tableview –

0

为什么不考虑在代码中创建一个完整的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];