@artursk

Как удалить элементы из двумерного массива?

Как удалить элементы из двумерного массива,exersise.removeAtIndex(indexPath.row) работает не постоянно, переодически падает и выдает ошибку fatal error: Array index out of range

var exersise = [(name: String, image:String, checked: Bool)]()

struct Objects {

var sectionName: String!
var sectionObjects: [(name: String, image:String, checked: Bool)]!
}
var objectsArray = [Objects]()

override func viewDidLoad() {
super.viewDidLoad()

objectsArray = [Objects(sectionName: "Standing", sectionObjects: [
    (name: "Приседания", image:"bb", checked: false),//and other]),
    Objects(sectionName: "Sitting", sectionObjects: [
        (name: "БЕГ", image:"ru", checked: false),//and other]),
    Objects(sectionName: "Special", sectionObjects: [
        (name: "БЕГ", image:"ru", checked: false),//and other])]
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return objectsArray.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
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.objectsArray[indexPath.section].sectionObjects[indexPath.row].checked = true
  exersise.append(objectsArray[indexPath.section].sectionObjects[indexPath.row])//добавляем в массив элементы ячейки 
} else {
    cell!.accessoryType = .None
self.objectsArray[indexPath.section].sectionObjects[indexPath.row].checked = false
    exersise.removeAtIndex(indexPath.row) //Удаляем добавленные элементы из массива. Не работает, приложение падает fatal error: Array index out of range
}
}
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 = self.objectsArray[indexPath.section].sectionObjects[indexPath.row].checked ? .Checkmark : .None
 return cell
 }
 override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
 return objectsArray[section].sectionName
 }
 func indexPathsForSelectedRowsInSection(section: Int) -> [NSIndexPath]? {
 return (tableView.indexPathsForSelectedRows!).filter({ (indexPath) -> Bool in
  • Вопрос задан
  • 328 просмотров
Пригласить эксперта
Ваш ответ на вопрос

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

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