如何设置选择TableView的一些索引

时间:2017-07-16 05:45:49

标签: ios swift uitableview

我有一个包含10行的UITableView。我希望默认选择第一行和第三行。有人能帮助我吗?

我尝试过的代码:

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)
    }
}

1 个答案:

答案 0 :(得分:4)

首先在storyboard中选择multipleSelection checkmark,或者在代码self.testTableView.allowsMultipleSelection = true中选择,然后在tableview.reloadData之后输入这2行

    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)

标准代码

    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)

指定此示例代码

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

希望这有帮助

相关问题