删除按钮不起作用,collectionView [英] Delete button not working, collectionView
问题描述
我有一个包含3个部分的collectionView和标题上的一个按钮,用于删除每个部分。我编写了代码,但始终收到此错误:
I have a collectionView with 3 sections and a button on the header to delete each section. I wrote my code but I keep getting this error:
严重错误:索引超出范围(lldb)
fatal error: Index out of range (lldb)
但是我不确定这是怎么回事?
But I'm not sure what is going on? Why isn't it working.
这是我的代码:
//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()
}
})
}
}
}
这也向我显示。
推荐答案
首先,您的代码与面向对象不同。这里有一些问题:
First of all, your code is nothing like Object-oriented. Here are some problems:
- 您在一个部分中对单元格进行了硬编码: 6 。宠物小精灵类类型的宠物小精灵数量不同时,您将如何处理?
- 您的代码中有许多重复项必须避免。如果section == 控件,有很多
;
- You have hard-coded number of cells in a section: 6. How will you handle the case pokemon class types have different number of pokemons?
- Your code has many duplications that must be avoided. There are lots of
if section ==
controls; which is a clear indicator of avoiding OO principles.
无论如何; 因为您有比无法使用的删除按钮更大的问题;我决定为您建立一个干净的项目,以说明如何以更加面向对象的方式解决该问题。我创建了诸如Pokemon和PokemonClass之类的域实体,并将相应的属性存储在这些实体中。这样我避免了控制器类中存在的许多代码重复。我还向您说明了如何使删除按钮正常工作(顺便说一句;当然,有更好的方法来处理此删除节的功能;但是我没有足够的时间来搜索它,而我是通过第一种方法来完成此操作的。在我心里)。由于时间限制,我不再处理口袋妖怪的图像。无论如何;请查看我在 github存储库中共享的源代码。您可以提出任何问题,当然可以自由使用我提供的任何代码。希望这将帮助您开始以面向对象的方式进行设计。
Anyway; since you have much bigger problems than non-working delete button; I have decided to set up a clean project for you to illustrate how to approach the problem in a more Object-oriented way. I have created domain entities such as Pokemon, and PokemonClass and stored corresponding attributes inside these entities. By this way; I have avoided many code duplication existing in your controller class. I have also illustrated you how to get delete button working (By the way; there sure are better ways to handle this deleting a section functionality; but I don't have enough time to search for it and I did it the first way that comes to my mind). I did not deal with images of the pokemons again due to time limitations. Anyway; look at the source code I have shared at my github repository. You can ask any questions you have and you are of course free to use any code I have provided. Hope this will help you getting started to design in an OO way.
这篇关于删除按钮不起作用,collectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!