Celdas UICollectionView con imágenes dentro del prototipo UITableView
ACTUALIZACIÓN: Resolví mi problema principal de que las imágenes correctas no se cargaran hasta que se desplazara en el collectionView. Agregué un collectionView.reloadData () a tableView: cellForRowAtIndexPath. También realicé algunos cambios para precargar la matriz de secuencia, en lugar de construirla mientras me desplazaba por la tabla (tableView: cellForRowAtIndexPath).
Agregó las actualizaciones a GitHub si está interesado.
https://github.com/Druiced/OpenDeck
Haré un seguimiento una vez que descubra cómo evitar que la aplicación se bloquee cuando se coloca un valor dinámico en el retorno (si configuro esto en 15, la aplicación no se bloqueará):
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return count(Array(sequenceArray[collectionView.tag])) / 2
}
POSTE ORIGINAL: solicitud de orientación.
Este tutorial me ayudó a darme cuenta de que esto tiene que ver con mi DataSource / Delegate. El autor construye la celda con addSubview en lugar de aprovechar la celda prototipo Xcode, que parece una cosa genial, así que estoy tratando de hacerlo.http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
Cualquier crítica sobre mi enfoque o el incumplimiento de las mejores prácticas es bienvenida.
Cada celda de la tabla tiene una UICollectionView. Cada celda en la Vista de colección muestra una imagen en el orden de la cadena "Secuencia" guardada. ejemplo: enlace "ADKDQDJDTD" hasta AD.png KD.png QD.png JD.png TD.png
Tengo dos problemas que parece que no puedo superar.
numberOfItemsInSection se vuelve loco cuando el número de tarjetas está controlado por la longitud de la matriz (return handArray.count / 2). Si coloco un número fijo, la aplicación funcionará, pero no muy hábil.Cuando aparece la tabla por primera vez, las tarjetas correctas no se muestran hasta que me desplazo hacia arriba y hacia abajo. También parece que los datos de cada CollectionView se cruzan cuando se muestran las tarjetas incorrectas al desplazarse hacia arriba y hacia abajo rápidamente.Estoy casi seguro de que esto tiene que ver con la configuración de mi fuente de datos.
DeckTableViewController.swift
import UIKit
import Parse
var deviceID: String?
var noRefresh: Bool?
var sequenceArray: Array<Character>?
class DeckTableViewController: UITableViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var handArray: Array<Character>!
var timeLineData:NSMutableArray = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
noRefresh = false
deviceId = UIDevice.currentDevice().identifierForVendor.UUIDString
}
override func viewDidAppear(animated: Bool) {
if noRefresh == false {
loadData()
noRefresh = true
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return timeLineData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:DeckTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! DeckTableViewCell
let deck:PFObject = timeLineData.objectAtIndex(indexPath.row) as! PFObject
cell.collectionView.dataSource = self
cell.collectionView.delegate = self
let sequenceTemp = deck.objectForKey("Sequence") as! String
handArray = Array(sequenceTemp)
cell.sequenceId.setTitle(deck.objectId, forState: UIControlState.Normal)
cell.cardCountLabel.text = "\((count(sequenceTemp)/2))"
// Date to String Stuff
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "(MM-dd) hh:mm:ss"
cell.timeLabel.text = dateFormatter.stringFromDate(deck.updatedAt!)
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.itemSize = CGSizeMake(99, 140)
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
cell.collectionView.collectionViewLayout = layout
return cell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return handArray.count / 2
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell:TableCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! TableCollectionViewCell
var bcolor : UIColor = UIColor.orangeColor()
cell.layer.borderColor = bcolor.CGColor
cell.layer.borderWidth = 2
cell.layer.cornerRadius = 3
var firstLetter: Character!
var secondLetter: Character!
//Building card file names from Sequence data
if (indexPath.row * 2) + 1 <= handArray.count {
firstLetter = handArray[indexPath.row * 2]
secondLetter = handArray[indexPath.row * 2 + 1]
let imageNameString = "\(firstLetter)\(secondLetter).png"
let front = UIImage(named: imageNameString)
cell.ImageView.backgroundColor = UIColor.orangeColor()
cell.ImageView.image = front
}
return cell
}
DeckTableViewCell.swift
import UIKit
class DeckTableViewCell: UITableViewCell, UITextViewDelegate {
@IBOutlet var collectionView: UICollectionView!
@IBOutlet var sequenceId: UIButton!
@IBOutlet var timeLabel: UILabel!
@IBOutlet var cardCountLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
TableCollectionViewCell.swift
importar UIKit
class TableCollectionViewCell: UICollectionViewCell {
@IBOutlet var ImageView: UIImageView!
}
Para este ejemplo, configuré (return handArray.count / 2) a 10 y cargué 3 secuencias. El número en el centro superior representa el número de tarjetas para cada fila. Tenga en cuenta que CollectionView no se actualiza con las tarjetas correctas, está recogiendo datos de los otros CollectionViews. SI agrego muchas más secuencias a esta mezcla, al desplazarme hacia arriba y hacia abajo, las tarjetas correctas LLEGARÁN A VECES, pero impredecibles.
Gracias por cualquier sugerencia, estoy feliz de volver al tablero de dibujo. Salud