Reputation: 657
I have a UITableView
in ViewController
which displays Default
, five times on running in the simulator based on the following code.
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var myTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! OutsideCell
myCell.titleLabel.text = "Default"
return myCell
}
@IBAction func firstButtonPressed(_ sender: Any){
}
@IBAction func secondButtonPressed(_ sender: Any) {
}
}
I also have 2 buttons at the bottom of ViewController
and
I like to display text as First Button
when firstButtonPressed
and Second Button
when secondButtonPressed
instead of Default
in UITableView
. How is this possible?
Upvotes: 0
Views: 656
Reputation: 760
You should use a generic method as eventButtonPressed
and use the tag attribute to determine which button is pressed through the storyboard.
@IBAction func eventButtonPressed(_ sender: UIButton) {
if (sender.tag == 0) {
//First Button
} else if (sender.tag == 1) {
//Second Button
}
}
You can use switch operand if you have many cases.
switch sender.tag {
case 0:
print("First button pressed")
case 1:
print("Second button pressed")
default:
print("Default button pressed")
}
Hope it helps,
Upvotes: 0
Reputation: 3599
You can loop through each cell of the table view when the button is tapped.
for cell in myTableView.visibleCells() {
cell.titleLabel?.text = "First Button"
}
Upvotes: 2