数组无法正确删除元素4

时间:2018-06-05 17:26:01

标签: arrays swift uitableview

在我的应用程序中,我使用tableView来显示日期出去的选项。为此,我有几个groupssubgroups

小组和小组是:

var mainGroups : [String] = ["Out On The Town", "Night In"]
let subGroups : [String] = ["Restaurants", "Activities", "Laid Back"]
let restaurantSubGroups : [String] = ["Dining", "Fast Food", "Desserts"]

因此,如果单击Restaurant单元格,它将附加mainGroup,创建下方带有restaurantSubGroup字符串的单元格,如果再次单击,则会删除这些单元格。

要删除,我会检查之前是否已单击该单元格。如果有,我从mainGroup中删除restaurantSubGroups字符串并相应地更新单元格。很简单吧?

阵列出现问题。由于我找不到的原因,它会从数组中删除diningdesserts,但会跳过Fast Food并删除Laid Back

以下是代码:

//Bools declared as class variables
var outOnTheTownIsActive : Bool = false
var restaurantIsActive : Bool = false

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
    cell.textLabel?.text = "\(mainGroups[indexPath.row])"

    if mainGroups.contains((cell.textLabel?.text)!) {
        cell.textLabel?.textAlignment = .left
    }
    if subGroups.contains((cell.textLabel?.text)!) {
        cell.textLabel?.textAlignment = .center
    }
    if restaurantSubGroups.contains((cell.textLabel?.text)!) || activitySubGroups.contains((cell.textLabel?.text)!) || laidbackSubGroups.contains((cell.textLabel?.text)!) {
        cell.textLabel?.textAlignment = .right
    }
    cell.selectionStyle = .none
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    let cellText = cell?.textLabel?.text

    switch cellText {
    case "Out On The Town":
        print("\(String(describing: cell?.textLabel?.text)) is clicked and row is \(indexPath.row)")
        if outOnTheTownIsActive {
            alterArray(starting: 2, ending: 4)
            addDeleteCells(starting: 1, ending: 3, indexPath: indexPath, add: false)
            outOnTheTownIsActive = false
        } else {
            mainGroups.insert(contentsOf: subGroups, at: indexPath.row+1)
            addDeleteCells(starting: 1, ending: 3, indexPath: indexPath, add: true)
            outOnTheTownIsActive = true
        }

    case "Restaurants":
       // print("\(String(describing: cell?.textLabel?.text)) is clicked and row is \(indexPath.row)")
        if restaurantIsActive {
            print("The items in the array are \(mainGroups) and i am removing \(mainGroups[2]), \(mainGroups[3]), and \(mainGroups[4])")
            alterArray(starting: 2, ending: 4)
            addDeleteCells(starting: 1, ending: 3, indexPath: indexPath, add: false)
            restaurantIsActive = false
            print("The new array is \(mainGroups)")
        } else {
            mainGroups.insert(contentsOf: restaurantSubGroups, at: indexPath.row+1)
            addDeleteCells(starting: 1, ending: 3, indexPath: indexPath, add: true)
            restaurantIsActive = true
        }

     //   print("There are \(mainGroups.count) in the array and they are \(mainGroups)")

    case "Dining":
        getAnnotations(query: "Restaurant", category: .restaurants, price: .twoDollarSigns)

    case "Fast Food":
        getAnnotations(query: "Fast Food", category: .food, price: nil)

    case "Desserts":
        getAnnotations(query: "Ice Cream", category: .food, price: nil)
}

func addDeleteCells(starting : Int, ending: Int, indexPath : IndexPath, add : Bool) {
    for i in starting...ending {
        let iPath = IndexPath(row: indexPath.row+i, section: 0)
        indexPaths.append(iPath)
    }
    if add {
        annotationTableView.beginUpdates()
        annotationTableView.insertRows(at: indexPaths, with: .automatic)
        annotationTableView.endUpdates()
    } else {
        annotationTableView.beginUpdates()
        annotationTableView.deleteRows(at: indexPaths, with: .automatic)
        annotationTableView.endUpdates()
    }
    indexPaths.removeAll()
}

func alterArray(starting: Int, ending : Int) {
    for i in starting...ending {
        print("In alterArray and removing \(mainGroups[i])")
        mainGroups.remove(at: i)
    }
}

这里是控制台打印出来的时候点击" Out on the Town"一次又一次"餐厅"两次:

The items in the array are ["Out On The Town", "Restaurants", "Dining", "Fast Food", "Desserts", "Activities", "Laid Back", "Night In"] and i am removing Dining, Fast Food, and Desserts
In alterArray and removing Dining
In alterArray and removing Desserts
In alterArray and removing Laid Back
The new array is ["Out On The Town", "Restaurants", "Fast Food", "Activities", "Night In"]

有任何想法可以跳过Fast Food并删除Laid Back吗?

需要注意的是:正在正确添加和删除单元格,但由于未正确删除数组的内容,当单元格离开屏幕并返回时,将使用不正确的文本重新创建。 / p>

1 个答案:

答案 0 :(得分:0)

因为当第一个循环结束时,数组索引发生了变化

 mainGroups.remove(at: i)

因此要删除的项目将处于低于应删除的项目的索引(-1)

第一个循环结束用餐已删除,因此

["Out On The Town", "Restaurants", "Dining",
"Fast Food", "Desserts", "Activities", "Laid Back", "Night In"]

将(您希望快餐下一个)

["Out On The Town", "Restaurants", "Fast Food",
"Desserts", "Activities", "Laid Back", "Night In"]

但索引3现在包含甜点