按下按钮插入表格视图行

时间:2018-09-02 16:30:27

标签: ios swift

我用两个控制器创建了一个UITabBarController,因为首先我有一个 带有UIButton的ViewController,第二个是UITableViewController,如何通过在ViewController中按btn来在tableView中插入新行? 是否应该使用自定义委派完成? 有人可以举个简单的例子吗

这是我的代码

import UIKit

class Home: UIViewController {

    var delegate: TableViewDelegate?

    lazy var addButton : UIButton = {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "plus_photo"), for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.addTarget(self, action: #selector(setupRows), for: .touchUpInside)
        return button

    }()

    @objc func setupRows() {    
        delegate?.insertingRows()            
    }

    override func viewDidLoad() {            
        super.viewDidLoad()

        delegate = self as? TableViewDelegate

        setupRows()

        navigationItem.title = "Test"

        view.backgroundColor = .white            
        view.addSubview(addButton)

        addButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        addButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        addButton.heightAnchor.constraint(equalToConstant: 150).isActive = true
        addButton.widthAnchor.constraint(equalToConstant: 150).isActive = true    
    }        
}

import UIKit

protocol TableViewDelegate {

    func insertingRows()
}

class TableViewTest: UITableViewController , TableViewDelegate {

    var items = ["abc", "abcd", "abcde"]
    let cellid = "cellid"

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(CustomCell.self, forCellReuseIdentifier: cellid)            
        insertingRows()    
    }

    func insertingRows() {

        let indexPath = IndexPath(row: items.count + 1, section: 0)

        tableView.insertRows(at: [indexPath], with: .left)
        self.tableView.reloadData()    
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: cellid, for: indexPath) as! CustomCell
        cell.textLabel?.text = items[indexPath.row]

        return cell
    }

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

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }        
}

class CustomCell: UITableViewCell {

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupViews() {            
    }    
}

1 个答案:

答案 0 :(得分:0)

您可以在Home viewController中使用 tabBar(_:didSelect:)委托方法。

您的家庭VC:

class Home: UIViewController, UITabBarControllerDelegate {

    private var selectedIndex: Int?

    override func viewDidLoad() {

        super.viewDidLoad()
        self.tabBarController?.delegate = self
    }

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

        if tabBarController.selectedIndex == 1 {

            if let secondViewController = viewController as? TableViewTest, let index = selectedIndex {
                secondViewController.insertingRows(index: index)
            }
        }
    }

    @objc func setupRows() {
        selectedIndex = 3
    }
}

您的TableViewTest TableViewController:

class TableViewTest: UITableViewController {

    func insertingRows(index: Int) {

        let indexPath = IndexPath(row: index, section: 0)

        tableView.beginUpdates()
        tableView.insertRows(at: [indexPath], with: .none)
        tableView.endUpdates()
    }
}