- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含 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()
}
})
}
}
}
它还向我展示了这一点。
最佳答案
首先,您的代码与面向对象的完全不同。这里有一些问题:
if section ==
控件;这是避免 OO 原则的明确指标。无论如何;因为你有比非工作删除按钮更大的问题;我已经决定为您建立一个干净的项目来说明如何以更面向对象的方式解决问题。我已经创建了域实体,例如 Pokemon 和 PokemonClass,并在这些实体中存储了相应的属性。通过这种方式;我已经避免了您的 Controller 类中存在的许多代码重复。我还向您展示了如何让删除按钮正常工作(顺便说一句;肯定有更好的方法来处理这个删除部分功能;但我没有足够的时间来搜索它,我在第一种方法中做到了在我心里)。由于时间限制,我没有再次处理口袋妖怪的图像。反正;查看我在我的 github repository 上分享的源代码.您可以提出任何问题,当然您可以自由使用我提供的任何代码。希望这会帮助您开始以 OO 方式进行设计。
关于arrays - 删除按钮不起作用,collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944113/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!