2017-07-16 33 views
1

我有一個UITableView 10行。我希望第一行和第三行被默認選中。有人能幫助我嗎?如何設置選定的一些索引的TableView

碼我曾嘗試:

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

,操縱細胞上'indexPath.row == 0 && indexPath.row == 2'在'cellForRowAt indexPath :'。 –

+0

RAJAMONAH-S,我知道如何找到indexPath,但我不知道如何選擇該行 – Jack

+0

@Jack當你在表格視圖中選擇行時,只有一行是select,所以你需要選擇兩行不是可能當你需要highlife行或區分行是可能的。 –

回答

3

首先在故事板上選擇multipleSelection複選標記,或通過代碼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

希望這有助於

+0

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell。我在這裏添加了你的代碼,但它沒有工作(我只用過其中的一個) – Jack

+0

@jack使用下面這個代碼你的tableView.reloadData –

+0

我不明白我必須把這個代碼) – Jack

相關問題