Я загружаю tableView из файла plist. Это работает без проблем. Я просто хочу "отметить" выбранные строки. На данный момент с моим кодом это не сработало так, как хотелось бы. На данный момент это выглядит так:
- нажмите row1 (будет отмечен ряд 1 = хорошо)
- снова нажмите row1 (ничего не происходит = плохо. Я ожидаю, что здесь строка не будет отмечена) При повторном нажатии на строку 1, это отменяет. После второго нажатия на него.
- когда я нажимаю row0 при начальной загрузке таблицы, он никогда не помечает меня строкой
мой код:
class portals: UITableViewController {
var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0)
...
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
cell.textLabel!.text = portals[indexPath.row]
return cell
}
// Check which portal is selected
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var whichPortalIsSelected: String = ""
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow();
// Tick the selected row
if indexPath!.row != lastSelectedIndexPath?.row {
let newCell = tableView.cellForRowAtIndexPath(indexPath!)
newCell?.accessoryType = .Checkmark
lastSelectedIndexPath = indexPath
whichPortalIsSelected = newCell!.textLabel!.text!
println("You selected cell #\(lastSelectedIndexPath.row)!") //PPP
println("You selected portal #\(whichPortalIsSelected)!") //PPP
// Un-Tick unselected row
} else {
let newCell = tableView.cellForRowAtIndexPath(indexPath!)
newCell?.accessoryType = .None
whichPortalIsSelected = newCell!.textLabel!.text!
println("You unselected cell #\(indexPath!.row)!") //PPP
println("You unselected portal #\(whichPortalIsSelected)!") //PPP
}
}
}