我正在做一个tableview
我希望能够轻按每个单元格,并在点击时在单元格上显示一个选中标记
现在,我有一些代码可以完成这项工作:
// checkmarks when tapped func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let section = indexPath.section let numberOfRows = tableView.numberOfRowsInSection(section) for row in 0..<numberOfRows { if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section)) { cell.accessoryType = row == indexPath.row ? .Checkmark : .None } } }
但是这段代码只选择一个部分中的1个单元格(我有5个部分)
我需要它来选择任何地方的任何单元格
另外,当我上下拖动屏幕时,我也会打勾
视图控制器
class ViewController: UIViewController, UITableViewDataSource { //class and subclass |) //---------------------------------------------------------------------------------------------------------------------------/ // Variable and constant, also IBAOutlet let section1 = ["this is used", "this is used to test", "this is used to test the lenght", "this is used to test the lenght of the text", "this is used to test the lenght of the text", "this is used to test the lenght of the text", "this is used to test the lenght of the text", "this is used to test the lenght of the text", "this is used to test the lenght of the text",] let section2 = ["this is used to test the lenght of the text"] let section3 = ["this is", "this is ",] @IBOutlet weak var scoreshow: UILabel! @IBOutlet weak var reset: UIButton! @IBOutlet weak var tableView: UITableView! // -------------------------------------------------------------------------------------- override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } //---------------------------------------------------------------------------------------- // checkmarks when tapped func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if let cell = tableView.cellForRowAtIndexPath(indexPath) { if cell.accessoryType == .Checkmark { cell.accessoryType = .None } else { cell.accessoryType = .Checkmark } } } //---------------------------------------------------------------------------------------- //number of sections for the table func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 5 } //---------------------------------------------------------------------------------------- //Calculate the amount of rows func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.section1.count; } //---------------------------------------------------------------------------------------- //Cells text label and config func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"cell") cell.textLabel!.text = section1[indexPath.row] cell.textLabel!.numberOfLines = 0 return cell } //---------------------------------------------------------------------------------------- @IBAction func resetswitch(sender: UIButton) { } //---------------------------------------------------------------------------------------- }
迅捷 > 3.0
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { cell.accessoryType = .none } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { cell.accessoryType = .checkmark } }
我通过使用两个Swift函数解决了:didSelectRowAtIndexPath和didDeselectRowAtIndexPath。
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { if let cell = tableView.cellForRowAtIndexPath(indexPath) { cell.accessoryType = .None } } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if let cell = tableView.cellForRowAtIndexPath(indexPath) { cell.accessoryType = .Checkmark } }
为了使其正常工作,请在您的cellForRowAtIndexPath函数中添加一行代码,以在屏幕上绘制表格视图时选择一行,否则,第一次选择另一行时,didDeselectRowAtIndexPath将不会被调用。像这样:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cellData", forIndexPath: indexPath) if (some condition to initially checkmark a row) cell.accessoryType = .Checkmark tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.Bottom) } else { cell.accessoryType = .None } return cell }