gpt4 book ai didi

arrays - 数组无法正确删除元素 swift 4

转载 作者:行者123 更新时间:2023-11-30 11:26:19 26 4
gpt4 key购买 nike

在我的应用程序中,我使用 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”一次,然后单击“Restaurants”两次时的控制台打印输出:

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

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

最佳答案

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

 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 现在包含甜点

关于arrays - 数组无法正确删除元素 swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50705766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com