Cookingcodes
Cookingcodes

Reputation: 23

Change tableView textlabel when button is clicked swift

How to change tableView textLabel when different buttons is clicked. I have two action buttons brandButton and profileButton, what I want to happen is when I click brandButton brandInformation will show up to the tableView textLabel, and when I click profileButton profileInformation will show up.

import UIKit

class StreamDetailController: UITableViewDataSource, UITableViewDelegate{

 @IBOutlet weak var tableViewController: UITableView!

   var brandInformation = ["Hat:","Top:","Pants:","Shoes"]
    var profileInformation = ["tim", "master"]

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableViewController.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.textLabel?.text = brandInformation[indexPath.row]
        return cell

   @IBAction func brandButton(_ sender: Any) {

    }
    @IBAction func profileButton(_ sender: Any) {

    }

Upvotes: 0

Views: 2710

Answers (1)

muescha
muescha

Reputation: 1539

Note:

in this line:

@IBOutlet weak var tableViewController: UITableView!

the variable is wrong and can be confusing. the tableViewController should be renamed to tableView

1. Solution

add a class var to hold which view you like to see and chenge it with the buttons. then call reloadData on the table to refresh the content:

    cell.textLabel?.text = brandInformation[indexPath.row]

to

var isShowBrand = true

// [...]

    if isShowBrand {
        cell.textLabel?.text = brandInformation[indexPath.row]
    } else {
        cell.textLabel?.text = profileInformation[indexPath.row]
    }

and also for the rowcount (you can also use the ternary operator:

    return isShowBrand ? brandInformation.count : profileInformation.count

(if you like to save it per cell then you need to save this info similar how you save the cell data var showBrand = [true, false] - but check that all 3 arrays have the same count of items to avoid index out of bounds errors)

2. Solution

just make a additional array tableData and you set in the buttons the array you need to see. and all data populating is done with the tableData array (you need to change all brandInformation to tableData)

var tableData = [String]()

// [...]


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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableViewController.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = tableData[indexPath.row]
    return cell
}
// [...]


@IBAction func brandButton(_ sender: Any){
    tableData = brandInformation
    tableViewController.reloadData()
}


@IBAction func profileButton(_ sender: Any){
    tableData = profileInformation
    tableViewController.reloadData()
}

Upvotes: 1

Related Questions