1

我想通过按下按钮在我的表格视图中添加一行,并且根据行高,滚动视图的内容大小必须增加,我还添加了一个视图作为 uiscrollview 的子视图,这是我的代码.....

import UIKit
class Create_StudioViewController: UIViewController {
    @IBOutlet var scrollview : UIScrollView!
    @IBOutlet var textfieldStartDate : UITextField!
    @IBOutlet var textfieldEndDate : UITextField!
    @IBOutlet var tableAddPackage : UITableView!
    @IBOutlet var buttonAddPackage : UIButton!
    @IBOutlet var buttonCreateStudio : UIButton!
@IBOutlet var viewonScroll : UIView!
@IBOutlet var labelAddPackage : UILabel!
@IBOutlet var viewTop : UIView!
var noofpackages : Int = 0
 var tableheight : Float = 0
var datePicker = UIDatePicker()
override func viewDidLoad() {
     super.viewDidLoad()
       noofpackages = 1
     tableheight = Float(self.tableAddPackage.frame.size.height)
     registernib()
     scrollview.contentSize = CGSize(width: 0, height: 1280)
     // Do any additional setup after loading the view.
}


func registernib(){
     let nibName = UINib(nibName: "AddpackageTableViewCell", bundle:nil)
     tableAddPackage.register(nibName, forCellReuseIdentifier: "Cell")
}
//tableview delegate and datasource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{

     return noofpackages
}

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell

{
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! AddpackageTableViewCell

     return cell

}
@IBAction func buttonAddClicked(){
     noofpackages += 1
     //table size inc
     self.tableAddPackage.frame = CGRect(x: 0, y: self.labelAddPackage.frame.origin.y + self.labelAddPackage.frame.size.height + 10 , width: UIScreen.main.bounds.width , height: self.tableAddPackage.frame.size.height + CGFloat(tableheight))

     //view on top of scroll size inc
     self.viewonScroll.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: self.viewonScroll.frame.size.height + CGFloat(tableheight)*2)
     //scrollview size inc.
     self.scrollview.contentSize = CGSize(width: 0, height: self.viewonScroll.frame.size.height + CGFloat(tableheight))
     // button coordinate change
     self.buttonAddPackage.center = CGPoint(x: self.buttonAddPackage.frame.origin.x, y: self.tableAddPackage.frame.origin.y + self.tableAddPackage.frame.size.height)



    //reload table
     tableAddPackage.reloadData()
    // self.viewonScroll.frame.height = self.viewonScroll.frame.height + self.tableAddPackage.frame.height



}
}
4

0 回答 0