When I press the button, I want to run the Tableview cellForRowAt function. But I don't want to call cellForRowAt when I don't press the button. Because the cellForRowAt function works as soon as the application opens. Press the button and if newdevicechipnumber.ishidden == false I want to run the function in . I want that function to work when I press the Newdeviceadd button.
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)
}
}
One way to do this would be to store some state in your ViewController and then flip that state once you push the button. If you do that, then you could tell your numberOfRowsInSection
function to return zero if the button has not been pressed yet and return the true number if the button as been pressed.
Option 1: You can use a bool
to trigger the population of your UITableView
. Let's say we call it shouldPopulate
, and we define it at the top of your controller.
var shouldPopulate: Bool = false
Use shouldPopulate
to trigger the correct number of rows or 0
.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return shouldPopulate ? yourNumberOfRows : 0
}
Then when your button is tapped, you define shouldPopulate
to true
and reload the data.
@IBAction func buttonTapped(_ sender: Any) {
self.shouldPopulate = true
self.tableView.reloadData()
}
Option 2: You can use a UIViewController
with a UITableView
inside if you want to get specific a behavior, it would be better than UITableViewController
.
Instead of setting the TableView Data Source
in viewDidLoad
for instance, define it inside your action function.
@IBAction func buttonTapped(_ sender: Any) {
self.tableView.dataSource = self
}
Setting data source should reload the data, you don't need to call reloadData()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.