NinjaDeveloper
NinjaDeveloper

Reputation: 1712

How to load related data to table when user click Ui button?

I have a view with 2 buttons (active and inactive) and a table view to show all my users. I want how can I load the related data if the user clicks one of the buttons using show active/inactive users?

import UIKit

class ViewController: UIViewController,UITableViewDataSource, UISearchBarDelegate {
    var users:[User] = []
    let user1 = User(username: "user1",email: "[email protected]",active: true)
    let user2 = User(username: "user2",email: "[email protected]",active: true)
    let user3 = User(username: "user3",email: "[email protected]",active: false)

    @IBOutlet weak var btnActiveUser: UIButton!
    @IBOutlet weak var btnInActiveUser: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        users.append(user1)
        users.append(user2)
        users.append(user3)
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  UITableViewCell()
        cell.textLabel?.text =  "this cell index for \(users[indexPath.row].username) "
        return cell
    }

    @IBAction func ActionShowActiveUsers(_ sender: Any) {
        let activeUser =  users.filter( { return $0.active == true } )
        print(activeUser.count)
    }

    @IBAction func btnShowInActiveUser(_ sender: Any) {
        let inactiveUser =  users.filter( { return $0.active == false } )
        print(inactiveUser.count)
    }
}

Upvotes: 0

Views: 102

Answers (4)

jMelvins
jMelvins

Reputation: 194

First at all you need several new variables:

enum FilterType { // you can add here any filter options you want
        case allUsers, active, inactive
    }

var users = [User]()
var activeUsers = [User]()
var inactiveUsers = [User]()
var filterType = FilterType.allUsers

After this edit numberOfRowInSection method:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch filterType {
        case .allUsers:
            return users
        case .active:
            return activeUsers
        case .inactive:
            return inactiveUsers
        }
}

And don't forget to add tableView.reloadData() in the end of your button actions:

@IBAction func ActionShowActiveUsers(_ sender: Any) {
        let activeUser =  users.filter( { return $0.active == true } )
        print(activeUser.count)
        filterType = .active
        tableView.reloadData()
    }

@IBAction func btnShowInActiveUser(_ sender: Any) {
        let inactiveUser =  users.filter( { return $0.active == false } )
        print(inactiveUser.count)
        filterType = .inactive
        tableView.reloadData()
}

Upvotes: 1

Robert Dresler
Robert Dresler

Reputation: 11140

As first, you need outlet for your table view, since you don't use UITableViewController

@IBOutlet weak var tableView: UITableView!

then inside viewDidLoad set its delegate and data source as your view controller

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    @IBOutlet weak var tableView: UITableView!
    ...
    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        tableView.delegate = self
        tableView.dataSource = self
    }
    ...
}

You can have two arrays. One for all users and one for filtered users by your condition (active or not)

var users = [User]()
var filteredUsers: [User] = users /* or just some other default value, e.g empty array */

This filtered array you can assign by filtered array every time you press certain button and don't forget to reload data of your table view then

@IBAction func ActionShowActiveUsers(_ sender: Any) {
    filteredUsers = users.filter { $0.active }
    tableView.reloadData()
}

@IBAction func btnShowInActiveUser(_ sender: Any) {
    filteredUsers = users.filter { !$0.active }
    tableView.reloadData()
}

then as data source array for your table view use filteredUsers

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell =  UITableViewCell()
    cell.textLabel?.text = "this cell index for \(filteredUsers[indexPath.row].username)"
    return cell
}

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need

var allUsers = [User]() 
var currentUsers = [User]()

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
    cell.textLabel?.text =  "this cell index for \(currentUsers[indexPath.row].username) "
    return cell
}

@IBAction func ActionShowActiveUsers(_ sender: Any) {
    currentUsers =  users.filter {$0.active}
    print(currentUsers.count)
    tableView.reloadData()
}

@IBAction func btnShowInActiveUser(_ sender: Any) {
    currentUsers  = users.filter { !$0.active }
    print(currentUsers.count)
    tableView.reloadData()
}

Add this in viewDidLoad

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

Upvotes: 3

emrepun
emrepun

Reputation: 2666

You can have a main array which holds all the users, and use another array as the dataSource of your tableView:

var mainUsers: [User] = []
var userDataSource: [User] = []

In viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()
    mainUsers.append(user1)
    mainUsers.append(user2)
    mainUsers.append(user3)

    userDataSource = mainUsers
}


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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  UITableViewCell()
        cell.textLabel?.text =  "this cell index for \(userDataSource[indexPath.row].username) "
        return cell
    }

Then in your action buttons:

    @IBAction func ActionShowActiveUsers(_ sender: Any) {
        userDataSource =  mainUsers.filter( { return $0.active == true } )
        tableView.reloadData()
    }

    @IBAction func btnShowInActiveUser(_ sender: Any) {
        userDataSource =  mainUsers.filter( { return $0.active == false } )
        tableView.reloadData()
    }

Upvotes: 1

Related Questions