【问题标题】:How to use checkmark for UITableView如何对 UITableView 使用复选标记
【发布时间】:2016-04-19 02:17:05
【问题描述】:

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

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()
}
}

【问题讨论】:

  • 您还需要更新您的数据模型,即 isSelected,因为当您滚动时这些复选标记会消失。

标签: ios iphone swift uitableview ios9


【解决方案1】:

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

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()
}

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

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

所以在您的数据模型中,添加新属性以保持复选标记状态,然后在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()
}

【讨论】:

  • 这个答案中的第一块代码不是应该怎么做的。事实上,它甚至不起作用。下半场更好。但是第二个didSelectRowAt 不应该重新加载整个表格视图。 Juts 在indexPath 重新加载一行。
【解决方案2】:

我假设您在情节提要中使用表格视图。如果是这种情况,在属性检查器中,您可以选择小鸡标记作为样式。

【讨论】:

    【解决方案3】:

    您需要UITableViewDelegate 协议的didSelectRowAtIndexPathdidDeselectRowAtIndexPath 方法。

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

    取消选择

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

    选择

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多