按按钮插入表视图行

问题描述 投票:0回答:1

我创建了一个带有两个控制器的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() {            
    }    
}
ios swift
1个回答
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()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.