A
A
artursk2016-01-08 14:57:25
iOS
artursk, 2016-01-08 14:57:25

How to remove elements from a 2D array?

How to remove elements from a two-dimensional array, exersise.removeAtIndex(indexPath.row) does not work all the time, crashes periodically and gives a 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

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question