gpt4 book ai didi

arrays - 删除按钮不起作用,collectionView

转载 作者:行者123 更新时间:2023-11-28 06:36:34 26 4
gpt4 key购买 nike

我有一个包含 3 个部分的 collectionView,标题上有一个用于删除每个部分的按钮。我写了我的代码,但我一直收到这个错误:

fatal error: Index out of range (lldb)

但我不确定发生了什么?为什么它不起作用。

这是我的代码:

//Global Identifier
private let cellIdentifier = "ImageCell"
private let headerIdentifier = "Header"


class ViewController: UICollectionViewController {

//Data Models

//Image Arrays
var fireImages: [UIImage] = [
UIImage(named: "charizard")!,
UIImage(named: "ninetails")!,
UIImage(named: "arcanine")!,
UIImage(named: "rapidash")!,
UIImage(named: "magmar")!,
UIImage(named: "flareon")!
]

var waterImages: [UIImage] = [
UIImage(named: "blastoise")!,
UIImage(named: "golduck")!,
UIImage(named: "cloyster")!,
UIImage(named: "goldeen")!,
UIImage(named: "magikarp")!,
UIImage(named: "vaporeon")!
]

var electricImages: [UIImage] = [
UIImage(named: "pikachu")!,
UIImage(named: "magneton")!,
UIImage(named: "zapdos")!,
UIImage(named: "electabuzz")!,
UIImage(named: "raichu")!,
UIImage(named: "jolteon")!
]

//Name Arrays
var fireNames = ["Charizard", "Ninetales", "Arcanine", "Rapidash", "Magmar", "Flareon"]

var waterNames = ["Blastoise", "Golduck", "Cloyster", "Goldeen", "Magikarp", "Vaporeon"]

var electricNames = ["Pikachu", "Magneton", "Zapdos", "Electrabuzz", "Raichu", "Jolteon"]

//Sections
var sectionTitle = ["Fire Types", "Water Types", "Electric Types"]


//--------------------------------

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

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

//--------------------------------


//MARK: - UICollectionViewDataSource

//Number of Sections
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return sectionTitle.count
}

//Number of Cells in each Section
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//How can I dynamically code this area?
return 6
}

//Header Configuration
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

if indexPath.section == 0 {

//Fire Type header
let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier, forIndexPath: indexPath) as! CollectionReusableView

header.headerTitle.text = sectionTitle[indexPath.section]
header.backgroundColor = UIColor.orangeColor()
header.deleteButton.tag = indexPath.section

return header

} else if indexPath.section == 1 {

//Water Type header
let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier, forIndexPath: indexPath) as! CollectionReusableView

header.headerTitle.text = sectionTitle[indexPath.section]
header.backgroundColor = UIColor.cyanColor()
header.deleteButton.tag = indexPath.section

return header

} else {

//Electric Type header
let header = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier, forIndexPath: indexPath) as! CollectionReusableView

header.headerTitle.text = sectionTitle[indexPath.section]
header.backgroundColor = UIColor.yellowColor()
header.deleteButton.tag = indexPath.section

return header

}

}

//Cell Configuration
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

if indexPath.section == 0 {

//Fire Type cells
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell

cell.pokemonImage.image = fireImages[indexPath.row]
cell.pokemonLabel.text = fireNames[indexPath.row]


return cell

} else if indexPath.section == 1 {

//Water Type cells
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell

cell.pokemonImage.image = waterImages[indexPath.row]
cell.pokemonLabel.text = waterNames[indexPath.row]

return cell

} else {

//Electric Type cells
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellIdentifier, forIndexPath: indexPath) as! CollectionViewCell

cell.pokemonImage.image = electricImages[indexPath.row]
cell.pokemonLabel.text = electricNames[indexPath.row]

return cell

}

}

//Delete Section Button
@IBAction func deleteSectionButton(sender: UIButton) {

//Section tag
let section = sender.tag

if section == 0 {

//Update data model
fireImages.removeAtIndex(section)
fireNames.removeAtIndex(section)
sectionTitle.removeAtIndex(section)

//Action
collectionView?.performBatchUpdates({
self.collectionView?.deleteSections(NSIndexSet(index: section))
},
completion: { (finished) in
if finished {
self.collectionView!.reloadData()
}
})

} else if section == 1 {

//Update data model
waterImages.removeAtIndex(section)
waterNames.removeAtIndex(section)
sectionTitle.removeAtIndex(section)

//Action
collectionView?.performBatchUpdates({
self.collectionView?.deleteSections(NSIndexSet(index: section))
},
completion: { (finished) in
if finished {
self.collectionView!.reloadData()
}
})

} else {

//Update data model
electricImages.removeAtIndex(section)
electricNames.removeAtIndex(section)
sectionTitle.removeAtIndex(section)

//Action
collectionView?.performBatchUpdates({
self.collectionView?.deleteSections(NSIndexSet(index: section))
},
completion: { (finished) in
if finished {
self.collectionView!.reloadData()
}
})

}

}

}

它还向我展示了这一点。

enter image description here

最佳答案

首先,您的代码与面向对象的完全不同。这里有一些问题:

  1. 您在一个部分中硬编码了单元格数量:6。您将如何处理 pokemon 类类型具有不同数量的 pokemon 的情况?
  2. 您的代码有许多必须避免的重复。有很多 if section == 控件;这是避免 OO 原则的明确指标。

无论如何;因为你有比非工作删除按钮更大的问题;我已经决定为您建立一个干净的项目来说明如何以更面向对象的方式解决问题。我已经创建了域实体,例如 Pokemon 和 PokemonClass,并在这些实体中存储了相应的属性。通过这种方式;我已经避免了您的 Controller 类中存在的许多代码重复。我还向您展示了如何让删除按钮正常工作(顺便说一句;肯定有更好的方法来处理这个删除部分功能;但我没有足够的时间来搜索它,我在第一种方法中做到了在我心里)。由于时间限制,我没有再次处理口袋妖怪的图像。反正;查看我在我的 github repository 上分享的源代码.您可以提出任何问题,当然您可以自由使用我提供的任何代码。希望这会帮助您开始以 OO 方式进行设计。

关于arrays - 删除按钮不起作用,collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944113/

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