【发布时间】:2018-02-18 03:48:19
【问题描述】:
更新:我解决了在 collectionView 上滚动之前无法加载正确图像的主要问题。我向 tableView:cellForRowAtIndexPath 添加了一个 collectionView.reloadData()。我还对预加载序列数组进行了一些更改,而不是在滚动表格时构建它(tableView:cellForRowAtIndexPath)。
如果您有兴趣,请将更新添加到 GitHub。
https://github.com/Druiced/OpenDeck
一旦我弄清楚如何防止应用程序在返回时放入动态值时崩溃(如果我将其设置为 15,应用程序将不会崩溃):
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return count(Array(sequenceArray[collectionView.tag])) / 2
}
原帖: 请求一些指导。
本教程帮助我意识到这必须与我的 DataSource/Delegate 有关。作者使用 addSubview 构建单元,而不是利用 Xcode 原型单元,这似乎是一件很酷的事情,所以我正在尝试这样做。 http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
欢迎对我的方法或未能遵循最佳实践提出任何批评。
表格中的每个单元格都有一个 UICollectionView。集合视图中的每个单元格按照保存的“序列”字符串的顺序显示一个图像。示例:“ADKDQDJDTD”链接到 AD.png KD.png QD.png JD.png TD.png
我有两个问题似乎无法解决。
- numberOfItemsInSection 会在卡片数量由数组长度驱动时变得古怪(返回 handArray.count / 2)。如果我输入一个固定的号码,该应用程序可以运行,但不是很流畅。
- 当表第一次出现时,直到我上下滚动表时才会显示正确的牌。每个 CollectionView 的数据似乎也在交叉路径,因为在快速上下滚动时会显示错误的卡片。
我几乎可以肯定这与我的数据源的设置方式有关。
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
导入 UIKit
class TableCollectionViewCell: UICollectionViewCell {
@IBOutlet var ImageView: UIImageView!
}
对于这个示例,我将 (return handArray.count / 2) 设置为 10 并加载了 3 个序列。 顶部中心的数字代表每行卡片的数量。 请注意,CollectionView 没有使用正确的卡片进行更新,它正在从其他 CollectionViews 中获取数据。如果我在此组合中添加更多序列,则在上下滚动时,有时会填充正确的卡片,但不可预测。
感谢您的任何建议,我很高兴回到绘图板上。干杯
【问题讨论】:
标签: ios iphone uitableview swift uicollectionview