当我按下tableview中的按钮时

问题描述 投票:-3回答:2

当我按下按钮时,我想运行Tableview cellForRowAt函数。但是,当我不按下按钮时,我不想调用cellForRowAt。因为cellForRowAt函数在应用程序打开后立即运行。按下按钮,如果newdevicechipnumber.ishidden == false我想运行该功能。当我按下Newdeviceadd按钮时,我希望该功能正常工作。

    class MainTableViewController: UITableViewController {

 let newdeviceadd: UILabel = {

        let topAlignment: SMIconLabel.VerticalPosition = .top

        let labelLeft = SMIconLabel()
        labelLeft.isHidden = true
        labelLeft.text = "Cihazı Ekle"
        labelLeft.font = UIFont(name: "Avenir-Black", size: 23)
        labelLeft.textColor = UIColor.flatWhite
        labelLeft.translatesAutoresizingMaskIntoConstraints = false
        labelLeft.icon = UIImage(named: "homepage")    // Set icon image
        labelLeft.iconPadding = 5                  // Set padding between icon and label
        labelLeft.numberOfLines = 0                // Required
        labelLeft.iconPosition = ( .left, topAlignment )
        labelLeft.textAlignment = .left
        return labelLeft
    }()

    var items: [Device] = []
        var itemsnew: [NewDevice] = []

        let cellId: String = "cellId"

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


            let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! DeviceTableViewCell
            let selectedIndexPaths = tableView.indexPathsForSelectedRows
            let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
            cell.accessoryType = rowIsSelected ? .checkmark : .none

            if newdevicechipnumber.isHidden == false {
                let deviceItemNew: NewDevice = itemsnew[indexPath.row]
                cell.new = deviceItemNew
                cell.titleNew.text = deviceItemNew.title
                cell.title1New.text = deviceItemNew.places
                cell.titlesaatNew.text = deviceItemNew.time
                cell.buttonNew.isOn = deviceItemNew.state
                cell.buttonNew.isHidden = hideBtn
                cell.buttonNew.addTarget(self, action: #selector(refreshDataNew), for: .touchUpInside)

             }

    }
swift uitableview
2个回答
0
投票

一种方法是在ViewController中存储一些状态,然后在按下按钮后再翻转该状态。如果你这样做,那么你可以告诉你的numberOfRowsInSection函数如果尚未按下按钮则返回零,如果按下按钮则返回真实数字。


0
投票

选项1:你可以使用bool来触发你的UITableView的人口。假设我们称之为shouldPopulate,我们将其定义在控制器的顶部。

var shouldPopulate: Bool = false

使用shouldPopulate触发正确的行数或0

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return shouldPopulate ? yourNumberOfRows : 0
}

然后,当您点击按钮时,将shouldPopulate定义为true并重新加载数据。

@IBAction func buttonTapped(_ sender: Any) {
    self.shouldPopulate = true
    self.tableView.reloadData()
}

选项2:如果你想获得特定的行为,你可以使用UIViewControllerUITableView,它会比UITableViewController更好。

例如,不要在TableView Data Source中设置viewDidLoad,而是在动作函数中定义它。

@IBAction func buttonTapped(_ sender: Any) {
    self.tableView.dataSource = self
}

设置数据源应重新加载数据,您不需要调用reloadData()

© www.soinside.com 2019 - 2024. All rights reserved.