删除按钮不起作用,collectionView

时间:2016-08-14 16:16:22

标签: arrays swift collectionview

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

  

致命错误:索引超出范围(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 个答案:

答案 0 :(得分:1)

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

  1. 您在一个部分中包含了硬编码的单元格数: 6 。你将如何处理案例口袋妖怪类类型有不同数量的小宠物?
  2. 您的代码有许多必须避免的重复。有很多if section ==个控件;这是避免OO原则的明确指标。
  3. 反正;因为你有比非工作删除按钮更大的问题;我已决定为您设置一个干净的项目,以说明如何以面向对象的方式解决问题。我已经创建了诸如Pokemon和PokemonClass之类的域实体,并在这些实体中存储了相应的属性。通过这种方式;我避免了控制器类中存在的许多代码重复。我还说明了如何使删除按钮工作(顺便说一句;确实有更好的方法来处理这个删除部分功能;但我没有足够的时间来搜索它,我做的第一种方式来在我心里)。由于时间限制,我没有再次处理小宠物的图像。无论如何;查看我在github repository分享的源代码。你可以提出任何问题,你当然可以自由使用我提供的任何代码。希望这能帮助您开始以OO方式进行设计。

相关问题