我对swift很新,希望对此主题有所帮助。我收到一个错误,说我的数组索引超出范围。错误是在第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 :(得分:0)
您正在将该行用作细分。这当然是出界的。
cell.myLabel?.text = hotelArray[indexPath.row][0].name
应该是
cell.myLabel?.text = hotelArray[indexPath.segment][indexPath.row].name
答案 1 :(得分:-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
}