Search code examples
swiftuitableviewtableviewfunc

Adding a Table View Cell when a button is pressed


I want my swift code to add a another tableview cell when the button is pressed. Right now there are 2 tableview cells. The button is the orange button that when pressed should add a 3rd green tabview cell. The func to add the 3rd cell is addCell. Look at numberOfRowsInSelection as well for the area where 2 cells are defined.

enter image description here

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource, UIImagePickerControllerDelegate & UINavigationControllerDelegate {
    var arr = [1,1,3,3]
    var tableview = UITableView()
    var arrayThatStartsEmptyImage = [UIImage]()
    var currentIndex = 0

    var startBtnpress = UIButton()


    var selectedIndexPath = IndexPath(row: 0, section: 0)
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 118
    }

   
    
    
   
    
    
    
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customtv
      
  
     
        
        return cell
    }
    
    @objc func addCell(){
        //add 3 cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        tableview.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height * 0.8)
        startBtnpress.frame = CGRect(x: 0, y: view.frame.height * 0.8, width: view.frame.width / 2  , height: view.frame.height / 5)
        view.addSubview(tableview)
        view.addSubview(startBtnpress)

        startBtnpress.backgroundColor = .orange
        tableview.register(customtv.self, forCellReuseIdentifier: "cell")
        tableview.delegate = self
        tableview.dataSource = self

      
 
        
    }
    
    
    
}

class customtv: UITableViewCell {
    lazy var backView : UIView = {
        let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width  , height: 110))
        view.backgroundColor = .green
        
        return view
    }()
    
    
    
    override func layoutSubviews() {
        backView.clipsToBounds = true
        backView.frame =  CGRect(x: 0, y: 6, width: bounds.maxX  , height: 110)
        
        
    }

    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(animated, animated: true)
        addSubview(backView)

    }
    
    
    
}

Solution

  • This has been asked a lot before...

    Instead of returning a hardcoded value inside tableView(_:numberOfRowsInSection:), you want to return the number of items in your data source (which I'm assuming is arr).

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    

    Then, simply add a row with

    func addRowToEnd() {
        arr.append(5) /// not sure what the numbers mean though...
        tableView.beginUpdates()
        tableView.insertRows(at: [IndexPath(row: arr.count - 1, section: 0)], with: .automatic) /// animate the insertion
        tableView.endUpdates() 
    }