ios – 在UITableView原型中包含图像的UICollectionView单元格

ios – 在UITableView原型中包含图像的UICollectionView单元格,第1张

概述更新:我解决了我的主要问题,正确的图像没有加载,直到在collectionView上滚动.我在tableView:cellForRowAtIndexPath中添加了一个collectionView.reloadData().我还做了一些更改来预加载序列数组,而不是在滚动表时构建它(tableView:cellForRowAtIndexPath). 如果您有兴趣,请将更新添加到GitHub. htt 更新:我解决了我的主要问题,正确的图像没有加载,直到在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 gets whacky when the number of cards is driven by the array length (return handarray.count / 2). If I place a fixed number the app will work,but not very slick. When the table first comes up,the correct cards do not display until I scroll up and down the table. It also appears the data for each CollectionVIEw is crossing paths as the wrong cards show up when scrolling up and down rAPIdly.

我几乎肯定这与我的数据源设置方式有关.

DecktableVIEwController.swift

import UIKitimport Parsevar 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 UIKitclass 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!}

对于这个例子,我设置(返回handarray.count / 2)为10并加载3个序列.
顶部中心的数字表示每行的卡数.
请注意,CollectionVIEw不会使用正确的卡进行更新,而是从其他CollectionVIEw中获取数据.如果我在这个组合中添加更多序列,当向上和向下滚动时,正确的卡将填充有时,但不可预测.

感谢您的任何建议,我很高兴回到绘图板.干杯

解决方法 好吧,让我们这样思考,你的DecktableVIEwController充当tablevIEw的数据源,DecktableVIEwCell充当集合视图的数据源.

考虑到上述事情,我们创建了一个示例项目
我没有深入,我正如你所经历的那样给出教程的例子

让我们使用单一视图应用程序和VIEwController创建一个示例项目
通过下面的代码,我获取了一个整数数组,其中包含一些值,表示在集合视图中显示多少个单元格.不要忘记添加tablevIEw并设置其数据源和deleagte.

在我们编写控制器类之前,我们需要一些类,如自定义tablevIEw单元格和自定义集合视图单元格,我们首先创建它们

创建一个新文件,它是UICollectionVIEwCell的子类,并将其命名为CustomCollectionVIEwCell并使用xib文件.

class CustomCollectionVIEwCell: UICollectionVIEwCell {@IBOutlet weak var aLabel: UILabel!  //to show the card number@IBOutlet weak var imageVIEw: UIImageVIEw! //imagevIEw i am setting it's background coloroverrIDe init(frame: CGRect) {    super.init(frame: frame)}required init(coder aDecoder: NSCoder) {    super.init(coder: aDecoder)}overrIDe func awakeFromNib() {    super.awakeFromNib()    }}

并按照上面的代码为标签和图像视图创建一个出口.

接下来,创建UItableVIEwCell的新文件子类,并将其命名为带有xib文件的CustomtableVIEwCell.打开CustomtableVIEwCell.xib文件并拖放集合视图并设置它的数据源并委托给单元而不是控制器.

并为集合视图创建一个插座,并将其命名为foldersCollectionVIEw.
 传递以下代码

import UIKitclass CustomtableVIEwCell: UItableVIEwCell,UICollectionVIEwDataSource,UICollectionVIEwDelegate {@IBOutlet weak var foldersCollectionVIEw: UICollectionVIEw!overrIDe init(frame: CGRect) {    super.init(frame: frame)}required init(coder aDecoder: NSCoder) {   // fatalError("init(coder:) has not been implemented")    super.init(coder: aDecoder)}var folderCount:Int?{    dIDSet(value)    {    }}overrIDe func awakeFromNib() {    super.awakeFromNib()    // Initialization code    //configure our collectionvIEw    var aFlowLayout : UICollectionVIEwFlowLayout = UICollectionVIEwFlowLayout()    aFlowLayout.scrollDirection = UICollectionVIEwScrollDirection.Horizontal    aFlowLayout.itemSize = CGSizeMake(60.0,90.0)    aFlowLayout.minimumlinespacing = 10.0    aFlowLayout.minimumInteritemSpacing = 0.0    aFlowLayout.sectionInset = UIEdgeInsetsMake(2,9,10)    foldersCollectionVIEw.collectionVIEwLayout = aFlowLayout    foldersCollectionVIEw.registerClass(CustomCollectionVIEwCell.self,forCellWithReuseIDentifIEr: "FolDER_CELL")    var cNib:UINib? = UINib(nibname: "CustomCollectionVIEwCell",bundle: nil)    foldersCollectionVIEw.registerNib(cNib,forCellWithReuseIDentifIEr: "FolDER_CELL")    foldersCollectionVIEw.frame = self.bounds}overrIDe func setSelected(selected: Bool,animated: Bool) {    super.setSelected(selected,animated: animated)    // Configure the vIEw for the selected state}class func CreateCustomCell() -> CustomtableVIEwCell{    var nibElements: Array = NSBundle.mainBundle().loadNibnamed("CustomtableVIEwCell",owner: self,options: nil)    var item: AnyObject?    for item in nibElements    {        if item is UItableVIEwCell        {            return item as CustomtableVIEwCell        }    }    return item as CustomtableVIEwCell}func collectionVIEw(collectionVIEw: UICollectionVIEw,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionVIEwCell {    var cell :CustomCollectionVIEwCell? = collectionVIEw.dequeueReusableCellWithReuseIDentifIEr("FolDER_CELL",forIndexPath: indexPath) as? CustomCollectionVIEwCell    //hear u can modify which image to be displayed in the collection vIEw cell    cell?.aLabel.text = "Card:\(indexPath.row)"    return cell!}func collectionVIEw(collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int {   return folderCount!}func numberOfSectionsInCollectionVIEw(collectionVIEw: UICollectionVIEw) -> Int {   return 1   }}

现在我们将代码转到VIEwController类
现在刚刚通过以下代码

class VIEwController: UIVIEwController,UItableVIEwDataSource,UItableVIEwDelegate {var cardCountArray:[Int] = []overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    // Do any additional setup after loading the vIEw,typically from a nib.    cardCountArray = [5,15,6,12,7,10]}overrIDe func dIDReceiveMemoryWarning() {    super.dIDReceiveMemoryWarning()    // dispose of any resources that can be recreated.}func numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int{    return cardCountArray.count}func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int{   return  1}func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell{    var cell:CustomtableVIEwCell? = tableVIEw.dequeueReusableCellWithIDentifIEr("CELL") as? CustomtableVIEwCell;    if(cell == nil)    {        cell = CustomtableVIEwCell.CreateCustomCell()    }    cell?.folderCount = cardCountArray[indexPath.section]    cell?.foldersCollectionVIEw.reloadData()    cell?.clipsToBounds = true    return cell!;}func tableVIEw(tableVIEw: UItableVIEw,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGfloat{    return 100.0}func tableVIEw(tableVIEw: UItableVIEw,vIEwForheaderInSection section: Int) -> UIVIEw? {    var headerVIEw:UIVIEw = UIVIEw(frame: CGRectMake(0,tableVIEw.bounds.size.wIDth,70.0))    var labelTitle:UILabel = UILabel(frame: CGRectMake(0,35))    var descriptionTitle:UILabel = UILabel(frame: CGRectMake(0,20,30))    headerVIEw.addSubvIEw(labelTitle)    headerVIEw.addSubvIEw(descriptionTitle)    labelTitle.text = "TOTAL_CARDS in section:\(section)"    descriptionTitle.text = "This CARD_SECTION contains \(cardCountArray[section]) CARDS"    return headerVIEw} func tableVIEw(tableVIEw: UItableVIEw,heightForheaderInSection section: Int) -> CGfloat {    return 50.0  } }

结果如下

如有任何遗漏,请告诉我

对于你的评论我有一个数组,例如,[“2C3C4C5C6C7C”,“AD2D3D4D5D”,“9H8H7H”]

为此你需要进行以下修改

//for first row u get like this//the string for the row is 2C3C4C5C6C7C//stringForCell = "2C3C4C5C6C7C"//2C//3C//4C//5C//6C//7C//for other cells u can get like below//the string for the row is AD2D3D4D5D//stringForCell = "AD2D3D4D5D"//AD//2D//3D//4D//5D//the string for the row is 9H8H7H//stringForCell = "9H8H7H"//9H//8H//7H//in controller controller class define array of stringclass VIEwController: UIVIEwController,UItableVIEwDelegate {var cardCountArray:[Int] = []var stringArray : [String] = []overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    // Do any additional setup after loading the vIEw,typically from a nib.    stringArray = ["2C3C4C5C6C7C","AD2D3D4D5D","9H8H7H"] }func numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int{   // return cardCountArray.count    return stringArray.count}func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell{    var cell:CustomtableVIEwCell? = tableVIEw.dequeueReusableCellWithIDentifIEr("CELL") as? CustomtableVIEwCell;    if(cell == nil)    {        cell = CustomtableVIEwCell.CreateCustomCell()    }    //cell?.folderCount = cardCountArray[indexPath.section]    cell?.stringForCell = stringArray[indexPath.section];    cell?.foldersCollectionVIEw.reloadData()    cell?.clipsToBounds = true    return cell!;}//in custom tablevIEw cell add a string variableclass CustomtableVIEwCell: UItableVIEwCell,UICollectionVIEwDelegate {@IBOutlet weak var foldersCollectionVIEw: UICollectionVIEw!var stringForCell:String = "" //add the string to hold the string//rest of the codefunc collectionVIEw(collectionVIEw: UICollectionVIEw,forIndexPath: indexPath) as? CustomCollectionVIEwCell    var str:Nsstring = stringForCell    var length = str.length    var totalLlength:Int =  length/2    var indexStart   = indexPath.row * (2);    var arange = NSMakeRange(indexStart,2)    var cardString:Nsstring = str.substringWithRange(arange)    println(cardString)    cell?.aLabel.text   = "Card: \(cardString)"    return cell!}func collectionVIEw(collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int {   println("the string for the row is \(stringForCell)")    var str:Nsstring = stringForCell    var length:Int = str.length    return length / 2   //return folderCount!}func numberOfSectionsInCollectionVIEw(collectionVIEw: UICollectionVIEw) -> Int { return 1}

我写了一篇关于how to add collection view inside custom table view cell的详细帖子听到希望这给出了比这篇文章更详细的解释.

总结

以上是内存溢出为你收集整理的ios – 在UITableView原型中包含图像的UICollectionView单元格全部内容,希望文章能够帮你解决ios – 在UITableView原型中包含图像的UICollectionView单元格所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1014215.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存