0
Aktivieren Sie in meinen Tabellenansichtszellen ein Häkchen, das nicht ein- oder ausgeschaltet wird. Kann nicht herausfinden warum. Egal, wie oft ich auf das Häkchen drücke, um es zu aktivieren oder zu deaktivieren, es ändert seinen Status nicht.Häkchen wird nicht ein-/ausgeschaltet
ChecklistViewController.swift
import UIKit
class ChecklistViewController: UITableViewController {
var items: [ChecklistItem]
required init?(coder aDecoder: NSCoder) {
items = [ChecklistItem]()
let row0item = ChecklistItem()
row0item.text = "Walk the dog"
row0item.checked = false
items.append(row0item)
let row1item = ChecklistItem()
row1item.text = "Brush my teeth"
row1item.checked = true
items.append(row1item)
let row2item = ChecklistItem()
row2item.text = "Learn iOS development"
row2item.checked = true
items.append(row2item)
let row3item = ChecklistItem()
row3item.text = "Soccer practice"
row3item.checked = false
items.append(row3item)
let row4item = ChecklistItem()
row4item.text = "Eat ice cream"
row4item.checked = true
items.append(row4item)
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem", forIndexPath: indexPath)
let item = items[indexPath.row]
let label = cell.viewWithTag(1000) as! UILabel
label.text = item.text
configureTextForCell(cell, withChecklistItem: item)
configureCheckmarkForCell(cell, withChecklistItem: item)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
let item = items[indexPath.row]
item.checked = !item.checked
item.toggleChecked()
configureCheckmarkForCell(cell, withChecklistItem: item)
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
func configureCheckmarkForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) {
if item.checked {
cell.accessoryType = .Checkmark
} else {
cell.accessoryType = .None
}
}
func configureTextForCell(cell: UITableViewCell, withChecklistItem item: ChecklistItem) {
let label = cell.viewWithTag(1000) as! UILabel
label.text = item.text
}
@IBAction func addItem() {
let newRowIndex = items.count
let item = ChecklistItem()
item.text = "I am a new row"
item.checked = false
items.append(item)
let indexPath = NSIndexPath(forRow: newRowIndex, inSection: 0)
let indexPaths = [indexPath]
tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
}
}
ChecklistItem.swift
Import Foundation
class ChecklistItem {
var text = ""
var checked = false
func toggleChecked() {
checked = !checked
}
}
Ja, das tat zu entfernen. Vielen Dank! – pdenlinger
Legen Sie im Zweifelsfall einige Haltepunkte fest und verwenden Sie den Befehl "po" (Ausdrucken). Sie können zu jedem Zeitpunkt in Ihrer App herausfinden, welche Werte verschiedene Dinge haben. – Ethan