Почему мои UICollectionViews все показывают одни и те же данные ячейки (последняя запись в массиве)?IOS

Программируем под IOS
Ответить Пред. темаСлед. тема
Anonymous
 Почему мои UICollectionViews все показывают одни и те же данные ячейки (последняя запись в массиве)?

Сообщение Anonymous »

Я использую 2 пользовательских класса, которые наследуют uicollectionView . Оба показывают аналогичную ошибку всех элементов в представлении коллекции, показывают последний элемент в массиве исходных источников.

Код: Выделить всё

class ClothingVC: UICollectionViewController {
let clothesList = [
Garment(itemID: 0, itemName: "Orange shirt 0", cost: 0, isOwned: true, image: UIImage(named: "placeholdershirt")!),
Garment(itemID: 1, itemName: "Skirt 1", cost: 0, isOwned: true, image: UIImage(named: "skirt1")!),
Garment(itemID: 2, itemName: "Skirt 2", cost: 0, isOwned: true, image: UIImage(named: "skirt2")!)
]

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return clothesList.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let clotheslistItem: Garment =  clothesList[indexPath.count]
let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "clothesCell", for: indexPath) as? ClothesCell
cell?.image.image = clotheslistItem.image
cell?.label.text = clotheslistItem.itemName
return cell!
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

@IBOutlet weak var clothesViewGrid: ClothesView!
override func viewDidLoad() {
super.viewDidLoad()
clothesViewGrid.reloadData()

}
}
< /code>
'' '< /p>
Позже у меня есть другой файл. Он продолжал высказываться, говоря об индексе за пределами границ, поэтому я добавил -1, чтобы остановить сбой, я знаю, что его не должно быть, но я также не знаю, почему у него проблемы с индексом. На этом VC показывает 3 ячейки, все названные Бетти. Я думаю, что единственная причина, по которой это не показывает, было то, что я добавил -с. 1. < /P>
class ProfileSelectorVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
let profiles = [
KidProfile(accountType: AccountType.child, name: "Amy", birthday: NSDate(), profilePicture: UIImage(named: "Panda_Ears")!, age: 10, pets: [], petBirthday: NSDate(), clothesInventory: [], allTimePoints: 0, currentPoints: 0, optionalSoundEffects: false),
KidProfile(accountType: AccountType.child, name: "Betty", birthday: NSDate(), profilePicture: UIImage(named: "Monkey_Ears")!, age: 10, pets: [], petBirthday: NSDate(), clothesInventory: [], allTimePoints: 0, currentPoints: 0, optionalSoundEffects: false),
KidProfile(accountType: AccountType.child, name: "Caty", birthday: NSDate(), profilePicture: UIImage(named: "Pig_Ears")!, age: 10, pets: [], petBirthday: NSDate(), clothesInventory: [], allTimePoints: 0, currentPoints: 0, optionalSoundEffects: false)
]

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return profiles.count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let profile: Profile =  profiles[indexPath.count-1]
let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "profilesReuseIdentifier", for: indexPath) as? ProfileCell
cell?.profileName.text = profile.name
cell?.profilePicture.image = profile.profilePicture
//cell?.profileName
return cell!
}
@IBOutlet weak var profilesOutlet: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
profilesOutlet.reloadData()

}
}
Почему все мои uicollectionview s, показывающие повторное содержание последней ячейки? Что не так с моим кодом? Также o Я продолжаю наносить проблемы индекса с профилями VC, когда у меня нет -1?>

Подробнее здесь: https://stackoverflow.com/questions/796 ... array-entr
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «IOS»