2016-04-19 55 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() }

相關問題