2016-04-19 39 views
0

我正在尝试为学习目的创建一个简单的待办事项列表应用程序。我希望能够点击一行并添加一个复选标记,当再次点击时,我希望它消失。我看了几个其他的例子,但没有任何工作。我怎样才能做到这一点?如何使用复选标记为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() 
} 
} 
+0

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

回答

0

简单地说,你可以使用此代码。

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

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

0

您需要didSelectRowAtIndexPathdidDeselectRowAtIndexPath方法的UITableViewDelegate协议。

你可以简单地使用它们!

取消

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

相关问题