Jack
Jack

Reputation: 321

How to set selected some index of TableView

I have a UITableView with 10 rows. I want the first and third row been selected by default. Could someone help me?

Code I have tried:

import UIKit

var variable = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

class ControllerClass: UIViewController, UITableViewDelegate, UITableViewDataSource {

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "intervalCellIdentifier", for: indexPath) as UITableViewCell
        cell.accessoryType = .none
        cell.textLabel?.text = variable[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    }

    override func viewWillAppear(_ animated: Bool) {
        self.tabBarController?.tabBar.isHidden = true
    }

    override func viewDidAppear(_ animated: Bool){
        super.viewDidAppear(animated)

        self.tableView.allowsMultipleSelection = true
        self.tableView.reloadData()
        // here is where selection is made
        self.tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
        self.tableView.selectRow(at: IndexPath(row: 2, section: 0), animated: false, scrollPosition: .none)
    }
}

Upvotes: 1

Views: 4097

Answers (1)

Reinier Melian
Reinier Melian

Reputation: 20804

First select multipleSelection checkmark in storyboard, or by code self.testTableView.allowsMultipleSelection = true, then after tableview.reloadData put this 2 lines

    self.testTableView.selectRow(at:  IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
    self.testTableView.selectRow(at:  IndexPath(row: 2, section: 0), animated: false, scrollPosition: .none)

Standard Code

    self.testTableView.reloadData()
    self.testTableView.allowsMultipleSelection = true
    //here is where selection is made
    self.testTableView.selectRow(at:  IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
    self.testTableView.selectRow(at:  IndexPath(row: 2, section: 0), animated: false, scrollPosition: .none)

Specified this Example Code

var variable = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

import UIKit

class ControllerClass: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var testTableView: UITableView!
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return variable.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath) as! TestTableViewCell
        cell.accessoryType = .none
        cell.lblWordText?.text = variable[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    }

    override func viewWillAppear(_ animated: Bool) {
        self.tabBarController?.tabBar.isHidden = true
    }
    override func viewDidAppear(_ animated: Bool){
        super.viewDidAppear(animated)
        self.testTableView.allowsMultipleSelection = true
        self.testTableView.reloadData()
        //here is where selection is made
        self.testTableView.selectRow(at: IndexPath(row: 0, section: 0), animated: false, scrollPosition: .none)
        self.testTableView.selectRow(at: IndexPath(row: 2, section: 0), animated: false, scrollPosition: .none)
    } 
    override func viewDidLoad() { 
        testTableView.dataSource = self 

    } 
}

enter image description here

Hope this helps

Upvotes: 4

Related Questions