点击行时启用/禁用按钮

时间:2015-05-29 22:45:37

标签: uitableview swift

如果选择override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var whatIsSelected: String = "" // Get Cell Label let indexPath = tableView.indexPathForSelectedRow() let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell! whatIsSelected = currentCell.textLabel!.text! // Unmark selected if (currentCell.accessoryType == UITableViewCellAccessoryType.Checkmark) { currentCell.accessoryType = UITableViewCellAccessoryType.None // Mark selected category } else { currentCell.accessoryType = UITableViewCellAccessoryType.Checkmark }` 中的最小一行,我想启用segue按钮。如果没有选择/标记行,也应该禁用该按钮。

目前我有这个代码而且我只能启用它但我不能再禁用它了:

a0  b0  c0  d0  e0
a1  b1  c1  d1  e1
a2  b2  c2  d2  
a3  b3  c3  d3  e3

1 个答案:

答案 0 :(得分:1)

更新:

取代我的回答。这是一个工作原型ViewController:

import UIKit

class ViewController: UITableViewController {

  @IBOutlet weak var testButton: UIButton!
  let testData = ["One", "Two", "Three", "Four", "Thirty two :)"]

  override func viewDidLoad() {
    tableView.allowsMultipleSelection = true
    testButton.enabled = false
  }

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
    currentCell.accessoryType = UITableViewCellAccessoryType.Checkmark
    testButton.enabled = true
  }

  override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
    currentCell.accessoryType = UITableViewCellAccessoryType.None
    let selectedRows = tableView.indexPathsForSelectedRows() as? [NSIndexPath]
    if selectedRows == nil {
      testButton.enabled = false
    }
  }

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

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let data = testData[indexPath.row]
    let cell =  UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
    cell.textLabel?.text = data
    return cell
  }
}
相关问题