如何为 UITableView 使用复选标记

How to use checkmark for UITableView

我正在尝试创建一个简单的待办事项列表应用程序以供学习之用。我希望能够单击一行并添加复选标记,再次单击时我希望它消失。我看过其他几个例子,但没有任何效果。我怎样才能做到这一点?

class FirstViewController: UIViewController, UITableViewDelegate,         UITableViewDataSource {

@IBOutlet weak var tbView: UITableView!



override func viewDidLoad() {
    super.viewDidLoad()
    tbView.reloadData()
}
override func viewWillAppear(animated: Bool) {
    self.tbView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

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

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default Tasks")

    //Assign the contents of our var "items" to the textLabel of each cell
    cell.textLabel!.text = tasks.manager[indexPath.row].name
    cell.detailTextLabel!.text = tasks.manager[indexPath.row].time

    return cell

}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){

    if (editingStyle == UITableViewCellEditingStyle.Delete){

        tasks.manager.removeAtIndex(indexPath.row)
        tbView.reloadData()
    }
}

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    cell!.accessoryType = .Checkmark
    tableView.reloadData()
}
}

我假设您在故事板中使用 table 视图。如果是这种情况,在属性检查器中,您可以选择一个小鸡标记作为样式。

您将需要 UITableViewDelegate 协议的 didSelectRowAtIndexPathdidDeselectRowAtIndexPath 方法。

您可以像这样简单地使用它们!

取消选择

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) cell!.accessoryType = .None //tableView.reloadData() }

Select

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) cell!.accessoryType = .Checkmark //tableView.reloadData() }

简单来说,你可以使用这个代码。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath)
    if (cell!.accessoryType == .Checkmark) {
        cell!.accessoryType = .None
    } else {
        cell!.accessoryType = .Checkmark
    }
    tableView.reloadData()
}

这样,当您重新select单元格时,复选标记会相应调整。

但是,您不应该以这种方式修改单元格复选标记,因为当您滚动时该单元格将被重新使用。 您需要更新您的数据模型,而不是上面的方法。

因此,在您的数据模型中,添加新的 属性 以保存复选标记状态,然后在 cellForRowAtIndexPath 函数中使用它。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Default Tasks")

    //Assign the contents of our var "items" to the textLabel of each cell
    cell.textLabel!.text = tasks.manager[indexPath.row].name
    cell.detailTextLabel!.text = tasks.manager[indexPath.row].time

    if tasks.manager[indexPath.row].isSelected { //assume that isSelected is bool
        cell!.accessoryType = .Checkmark
    } else {
        cell!.accessoryType = .None
    }

    return cell
}

然后在您的 didSelectRowAtIndexPath 中更新模型。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let dataModel = tasks.manager[indexPath.row]
    dataModel.isSelected = !dataModel.isSelected
    tableView.reloadData()
}