点击SWIFT时,将打开和closuresUItableviewcell对勾

我正在做一个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 } } } 

但是这段代码只select了一个段内的1个单元(我有5个段)

我需要它在任何地方select任何单元格

另外,当我拖动我的屏幕上下,我失去了复选标记

任何帮助将非常感激

viewcontroller.swift

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

我解决了使用两个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函数中添加一行代码来在表格视图绘制在屏幕上时select一行,否则didDeselectRowAtIndexPath将不会在你第一次select另一行时被调用。 像这样:

 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 } 

尝试这个:

 var checked = [Bool]() // Have an array equal to the number of cells in your table func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell //configure you cell here. if !checked[indexPath.row] { cell.accessoryType = .None } else if checked[indexPath.row] { cell.accessoryType = .Checkmark } return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if let cell = tableView.cellForRowAtIndexPath(indexPath) { if cell.accessoryType == .Checkmark { cell.accessoryType = .None checked[indexPath.row] = false } else { cell.accessoryType = .Checkmark checked[indexPath.row] = true } } } 

重置所有的checkbox:

 func resetChecks() { for i in 0.. < tableView.numberOfSections { for j in 0.. < tableView.numberOfRowsInSection(i) { if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: j, inSection: i)) { cell.accessoryType = .None } } } } 

Swift 3.0

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { cell.accessoryType = .checkmark } } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { if let cell = tableView.cellForRow(at: indexPath) { cell.accessoryType = .none } } 

Swift 3.0
只使用一个函数来保持简单

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) if let cell = tableView.cellForRow(at: indexPath as IndexPath) { if cell.accessoryType == .checkmark{ cell.accessoryType = .none } else{ cell.accessoryType = .checkmark } } } 

UITableView保持单个或多个select状态。 所以海事组织必须有一个很好的理由来保持整个并行状态。 如果您只想根据select状态更改单元格的外观,请在单元格中执行此操作。

在你的UITableViewCell子类中,覆盖setSelected像这样:

 override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) self.accessoryType = selected ? .Checkmark : .None } 

不需要使用任何表视图委托方法。

注意:您必须调用super.setSelected,否则单元格不会正确保持选定的状态。

Interesting Posts