2015-03-11 6 views
0

Im довольно новый для быстрой и хотел бы помочь на эту тему. Я получаю сообщение об ошибке, когда мой индекс массива выходит за пределы диапазона. ошибка в строке 85. Я знаю, в чем проблема, я просто не знаю, как ее исправить, и я потрачу несколько часов только на линии 85! Любая помощь ценится !!Индекс массива вне диапазона. ОШИБКА

import UIKit 

class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate { 


    var hotels = [Hotels]() 
    var hotelArray : [[Hotels]] = []; 


    override func viewDidLoad() { 
     super.viewDidLoad() 

    var hotel1 = Hotels(name: "Four Seasons Thailand", image: UIImage(named: "four1")!, rating: "Five Stars") 
    var hotel2 = Hotels(name: "Ritz Carlton Dubai ", image: UIImage(named: "ritz2")!, rating: "Five Stars") 
    var hotel3 = Hotels(name: "Four Seasons Miami", image: UIImage(named: "four3")!, rating: "Five Stars") 
    var hotel4 = Hotels(name: "Beverly Wilshire", image: UIImage(named: "bev4")!, rating: "Five Stars") 
    var hotel5 = Hotels(name: "Ritz Carlton Cancun", image: UIImage(named: "ritz5")!, rating: "Five Stars") 
    var hotel6 = Hotels(name: "Westin Diplomat", image: UIImage(named: "w1")!, rating: "Four Stars") 
    var hotel7 = Hotels(name: "W New Orleans", image: UIImage(named: "w2")!, rating: "Four Stars") 
    var hotel8 = Hotels(name: "W Savannah", image: UIImage(named: "w3")!, rating: "Four Stars") 
    var hotel9 = Hotels(name: "W Times Square", image: UIImage(named: "w4")!, rating: "Four Stars") 
    var hotel10 = Hotels(name: "W Miami", image: UIImage(named: "w5")!, rating: "Four Stars") 

    hotelArray = [ [hotel1, hotel2, hotel3, hotel4, hotel4],[hotel6, hotel7, hotel8, hotel9, hotel10] ] 


    } 



    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 

     return hotelArray.count 
    } 


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 


     return hotelArray[section].count 

    } 

    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, 

     atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { 
     // assume that it will be either a header or footer first. 
     var reuseIdentifier = "headerId"; 
     // now check to see if it's the type that we didn't assume it would be. 
     if kind == UICollectionElementKindSectionFooter { 
      // reassign the identifier 

     } 
     // dequeue the reusable view and give it information 
     let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "headerId", forIndexPath: indexPath) as CustomCollReusableCellCollectionReusableView; 

     // configure the headerFooter view 
     header.headerTitle.text = hotelArray[indexPath.section][0].rating 

     header.deleteButton?.tag = indexPath.section; 

     // return our view 
     return header; 
    } 





    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     //write here 
     let cell : CellCollectionCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellReuse", forIndexPath: indexPath) as CellCollectionCollectionViewCell; 

     //naming and setting the cells properties here!! 
     //cell.myLabel.text = "hello \(indexPath.row) S: \(indexPath.section)"; 

     cell.myLabel?.text = hotelArray[indexPath.row][0].name; 



     // return the cell 
     return cell; 
    } 





} 

ответ

0

Вы используете строку в качестве сегмента. Это, конечно, вне пределов.

cell.myLabel?.text = hotelArray[indexPath.row][0].name 

должен быть

cell.myLabel?.text = hotelArray[indexPath.segment][indexPath.row].name 
+0

Спасибо Хельге !!! Вот и все. –

+0

Добро пожаловать. Думайте, что вы должны указать мой ответ для ppl, который ищет тот же ответ. :) –

-1

Если я получаю это правильно, вы должны изменить это:

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 

     return hotelArray.count 
    } 


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 


     return hotelArray[section].count 

    } 

к этому:

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 

     return 1 
    } 


func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 


     return 10 

    } 
+0

Никос, спасибо за ответ. hotelArray - массив массивов. У меня должно быть два раздела и 5 ячеек в каждом разделе, и они должны быть динамичными, поэтому, когда добавляется больше отелей или создается больше секций, это все равно будет работать. Секции выглядят отлично, и все ячейки есть, но когда я пытаюсь назвать ячейки в строке 85, он говорит, что индекс отсутствует или диапазон. –

Смежные вопросы