@artursk

Как предотвратить дублирование чекмаркера в UITableView?

Ребята помогите плиз, у кого есть пять минут... Создал таблицу из трех секций, добавил нажатие чекмаркера. Не могу убрать дублирование чекмаркера в таблице на нижеследующих ячейках от нажатых. Делал все по курсу на ресурсе SwiftBook. Спасибо

import UIKit

class AllExersiseTableViewController: UITableViewController {
//создаем секции
struct Objects {
var sectionName: String!
var sectionObjects: [(name: String, image:String)]!
}
var objectsArray = [Objects]()
//создаем масив с ячейками для предотвращения дублирования чекмаркеров в таблице по нажатию
var allExersiseTable = [Bool](count: 15, repeatedValue: false)

override func viewDidLoad() {
super.viewDidLoad()

objectsArray = [Objects(sectionName: "Standing", sectionObjects: [(name: "Приседания", image:"bb"), (name: "Отжимания", image:"bt"), (name: "Подтягивания", image:"ca"), (name: "Прыжки", image:"co"), (name: "Бег", image:"de")]), Objects(sectionName: "Sitting", sectionObjects: [(name: "БЕГ", image:"ru"), (name: "ПРЫЖКИ", image:"al"), (name: "ПРИСЕДАНИЯ", image:"au"), (name: "ОТЖИМАНИЯ", image:"es"), (name: "ТОЛЧЕК", image:"fr")]), Objects(sectionName: "Special", sectionObjects: [(name: "БЕГ", image:"ru"), (name: "ПРЫЖКИ", image:"al"), (name: "ПРИСЕДАНИЯ", image:"au"), (name: "ОТЖИМАНИЯ", image:"es"), (name: "ТОЛЧЕК", image:"fr")])]
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return objectsArray.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return objectsArray[section].sectionObjects.count
}
//нажатие на ячейку
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)

if cell!.accessoryType == .None {
cell!.accessoryType = .Checkmark
self.allExersiseTable[indexPath.row] = true
} else {
cell!.accessoryType = .None
self.allExersiseTable[indexPath.row] = false
}
//убираем эфект нажатой ячейки
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
//отображаем элементы ячеек
cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row].name
cell.imageView?.image = UIImage(named: objectsArray[indexPath.section].sectionObjects[indexPath.row].image)
//для предотвращения дублирования чекмаркеров в таблице по нажатию
cell.accessoryType = allExersiseTable[indexPath.row] ? .Checkmark : .None
//цвет чекмаркера
cell.tintColor = UIColor.redColor()
return cell
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
return objectsArray[section].sectionName
}

}
  • Вопрос задан
  • 300 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы