Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios tableViewCell数据显示中的CollectionView_Ios_Swift_Uitableview_Uicollectionview - Fatal编程技术网

Ios tableViewCell数据显示中的CollectionView

Ios tableViewCell数据显示中的CollectionView,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我在每个tableViewCell中嵌入了collectionView。在每个tableViewWillDisplayCell事件上,我为collectionView设置了正确的数据源,然后在collectionView中重新加载数据 代码如下: override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPat

我在每个tableViewCell中嵌入了collectionView。在每个tableViewWillDisplayCell事件上,我为collectionView设置了正确的数据源,然后在collectionView中重新加载数据

代码如下:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    let c = cell as! CustomTableViewCell
    c.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, indexPath: indexPath)
}
CustomTableViewCell.swift:

class CustomTableViewCell: UITableViewCell {

var collectionView: indexedCollectionView!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    layout.itemSize = CGSizeMake(5, 5)
    layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
    self.collectionView = indexedCollectionView(frame: CGRectZero, collectionViewLayout: layout)
    self.collectionView.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: collectionViewCellIdentifier as String)
    self.collectionView.backgroundColor = UIColor.whiteColor()
    self.collectionView.showsHorizontalScrollIndicator = false
    self.contentView.addSubview(self.collectionView)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func layoutSubviews() {
    super.layoutSubviews()
    self.collectionView.frame = self.contentView.bounds;
}

func setCollectionViewDataSourceDelegate(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, indexPath: NSIndexPath){
    self.collectionView.dataSource = delegate
    self.collectionView.delegate = delegate
    self.collectionView.indexPath = indexPath
    self.collectionView.tag = indexPath.row
    self.collectionView.reloadData()
}

}
class CustomTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {

//var collectionView: UICollectionView!
@IBOutlet weak var collectionView: UICollectionView!

var event: Event?
var size: CGSize?

override func awakeFromNib() {
    super.awakeFromNib()
    println("awake")

    var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    layout.itemSize = CGSizeMake(5, 5)
    layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
    collectionView.collectionViewLayout = layout
    //self.collectionView = indexedCollectionView(frame: CGRectZero, collectionViewLayout: layout)
    collectionView.registerClass(CustomCollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView.backgroundColor = UIColor.whiteColor()
    collectionView.showsHorizontalScrollIndicator = false
    var cNib:UINib? = UINib(nibName: "CustomCollectionViewCell", bundle: nil)
    collectionView.registerNib(cNib, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView.frame = self.bounds
    //self.contentView.addSubview(self.collectionView)

}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

//        var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
//        layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
//        layout.itemSize = CGSizeMake(5, 5)
//        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
//        self.collectionView = indexedCollectionView(frame: CGRectZero, collectionViewLayout: layout)
//        self.collectionView.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: collectionViewCellIdentifier as String)
//        self.collectionView.backgroundColor = UIColor.whiteColor()
//        self.collectionView.showsHorizontalScrollIndicator = false
//        self.contentView.addSubview(self.collectionView)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

//    override func layoutSubviews() {
//        super.layoutSubviews()
//        self.collectionView.frame = self.contentView.bounds;
//    }

//    func setCollectionViewDataSourceDelegate(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, indexPath: NSIndexPath){
//        self.collectionView.dataSource = delegate
//        self.collectionView.delegate = delegate
//        self.collectionView.indexPath = indexPath
//        self.collectionView.tag = indexPath.row
//        self.collectionView.reloadData()
//    }

// MARK: - Collection view data source

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: CustomCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CustomCollectionViewCell
    //let tableViewCell = tableView!.cellForRowAtIndexPath(NSIndexPath(forItem: collectionView.tag, inSection: 0))
    var rect: CGRect?

    var label: UILabel = UILabel()
    label.font = UIFont(name: "HelveticaNeue-Light", size: 18.0)
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.ByWordWrapping
    label.textAlignment = .Center
    switch indexPath.row {
    case 0:
        cell.frame = CGRectMake(0, 0, size!.width * 0.2, size!.height)
        rect = cell.bounds
        label.frame = rect!
        label.text = event!.category
        break
    case 1:
        cell.frame = CGRectMake(size!.width * 0.2, 0, size!.width * 0.3, size!.height)
        rect = cell.bounds
        label.frame = rect!
        label.text = event!.event
        break
    case 2:
        cell.frame = CGRectMake(size!.width * 0.5, 0, size!.width * 0.26, size!.height)
        rect = cell.bounds
        label.frame = rect!
        label.text = event!.location
        break
    case 3:
        cell.frame = CGRectMake(size!.width * 0.76, 0, size!.width * 0.12, size!.height)
        rect = cell.bounds
        label.frame = rect!
        label.text = event!.date
        break
    case 4:
        cell.frame = CGRectMake(size!.width * 0.88, 0, size!.width * 0.12, size!.height)
        rect = cell.bounds
        label.frame = rect!
        label.text = event!.time
        break
    default:
        break
    }
    cell.addSubview(label)
    cell.layer.borderColor = UIColor.blackColor().CGColor
    cell.layer.borderWidth = 0.5

    return cell
}

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
}
}
当应用程序启动时,一切看起来都很好(),但当我滚动我的tableView时,触发tableView将再次为触发collectionView的重新加载数据的每个单元格显示cell事件

但是效果不好()。有人能帮我解决我的问题吗

更新:

我试图重新编写代码,但遇到了一个问题。这是我的全部代码:

class TableViewController: UITableViewController {//, UICollectionViewDataSource, UICollectionViewDelegate {

var events: [Event] = [
    Event(category: "GAA - Football Campionship", event: "Round 4A Munster Final Runner up v Round 3A Winner", location: "Semple Stadium - Thurles", date: "7/12/15", time: "2:00pm"),
    Event(category: "Football", event: "Round 4A Munster", location: "Thurles", date: "7/2/15", time: "All day"),
    Event(category: "Golf - 4 Majors", event: "U.S. Open Day 4 ( Final Day)", location: "Oakmont Country Club, Pennsylvania", date: "7/12/15", time: ""),
]

override func viewDidLoad() {
    super.viewDidLoad()

    //self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "Cell")
    self.tableView.separatorStyle = .None
    self.tableView.tableFooterView = UIView(frame: CGRectZero)
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return events.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell: CustomTableViewCell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? CustomTableViewCell

    if cell == nil {
        cell = CustomTableViewCell.CreateCustomCell()
    }
    cell!.event = events[indexPath.row]
    cell!.size = CGSize(width: cell!.frame.width, height: cell!.frame.height)
    cell!.collectionView.reloadData()
    cell!.clipsToBounds = true

    return cell!
}

//    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
//        let c = cell as! CustomTableViewCell
//        c.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, indexPath: indexPath)
//    }

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    var cellHeight: CGFloat = 0.0
    let screenWidth: CGFloat = UIScreen.mainScreen().bounds.width

    var question: NSString = events[indexPath.row].category
    var size = CGSizeMake(screenWidth * 0.2, 9999)
    var textRect = question.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!], context: nil)
    size = textRect.size
    cellHeight = size.height

    question = events[indexPath.row].event
    size = CGSizeMake(screenWidth * 0.3, 9999)
    textRect = question.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!], context: nil)
    size = textRect.size
    if cellHeight < size.height {
        cellHeight = size.height
    }

    question = events[indexPath.row].location
    size = CGSizeMake(screenWidth * 0.3, 9999)
    textRect = question.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!], context: nil)
    size = textRect.size
    if cellHeight < size.height {
        cellHeight = size.height
    }

    question = events[indexPath.row].date
    size = CGSizeMake(screenWidth * 0.1, 9999)
    textRect = question.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!], context: nil)
    size = textRect.size
    if cellHeight < size.height {
        cellHeight = size.height
    }

    question = events[indexPath.row].time
    size = CGSizeMake(screenWidth * 0.1, 9999)
    textRect = question.boundingRectWithSize(size, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 18.0)!], context: nil)
    size = textRect.size
    if cellHeight < size.height {
        cellHeight = size.height
    }

    return cellHeight
}
}
此外,在故事板中,我向tableViewCell添加了一个collectionView,将其连接到outlet变量,并将CustomCollectionCell类设置为故事板中的collectionCell

但当我启动应用程序时,它在TableViewCellForRowatineXpath的这一行抛出了一个异常:

2015-07-16 13:29:00.965 CollectionViewInTableViewCell[4241:1005396]由于未捕获的异常NSUnknownKeyException而终止应用程序,原因:[CollectionViewInTableViewCell.CustomTableViewCell 0x126e12440 setValue:forUndefinedKey:]:此类不符合密钥集合的键值编码。'


您的
CustomTableViewCell
必须实现
UICollectionViewDelegate
UICollectionViewDataSource
,在将单元格添加到表视图时,单元格本身必须链接到委派和数据源。

CustomTableViewCell
必须实现
UICollectionViewDelegate
UICollectionViewDataSource
以及单元格本身在将其添加到表视图时必须链接到委托和数据源。

检查我的答案听到我如何将数据从控制器传递到customTableViewCell以向collectionViewCells输入数据?只需向您的单元格添加数据属性即可自定义单元格并在控制器(CellForRowatineXpath)中进行设置。好的,我将尝试并告诉您是否出现问题。@mikle94在我的示例代码中,在
cellForAtIndexPath
方法中,我将数据传递给单元格,在单元格中,它将由集合视图使用,例如
cell?.folderCount=cardCountArray[indexPath.section]
检查我的答案听我如何将数据从控制器传递到customTableViewCell以将数据输入collectionViewCells?只需将数据属性添加到自定义单元格并在控制器(cellForRowAtIndexPath)中进行设置即可。好,如果出现问题,我会尝试告诉你。@mikle94在我的示例代码中,在
cellForAtIndexPath
方法中,我正在将数据传递给单元格,在单元格中,它将被集合视图使用,例如
cell?.folderCount=cardCountArray[indexPath.section]
class CustomTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {

//var collectionView: UICollectionView!
@IBOutlet weak var collectionView: UICollectionView!

var event: Event?
var size: CGSize?

override func awakeFromNib() {
    super.awakeFromNib()
    println("awake")

    var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
    layout.itemSize = CGSizeMake(5, 5)
    layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
    collectionView.collectionViewLayout = layout
    //self.collectionView = indexedCollectionView(frame: CGRectZero, collectionViewLayout: layout)
    collectionView.registerClass(CustomCollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView.backgroundColor = UIColor.whiteColor()
    collectionView.showsHorizontalScrollIndicator = false
    var cNib:UINib? = UINib(nibName: "CustomCollectionViewCell", bundle: nil)
    collectionView.registerNib(cNib, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView.frame = self.bounds
    //self.contentView.addSubview(self.collectionView)

}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

//        var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
//        layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
//        layout.itemSize = CGSizeMake(5, 5)
//        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
//        self.collectionView = indexedCollectionView(frame: CGRectZero, collectionViewLayout: layout)
//        self.collectionView.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: collectionViewCellIdentifier as String)
//        self.collectionView.backgroundColor = UIColor.whiteColor()
//        self.collectionView.showsHorizontalScrollIndicator = false
//        self.contentView.addSubview(self.collectionView)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

//    override func layoutSubviews() {
//        super.layoutSubviews()
//        self.collectionView.frame = self.contentView.bounds;
//    }

//    func setCollectionViewDataSourceDelegate(dataSourceDelegate delegate: protocol<UICollectionViewDelegate,UICollectionViewDataSource>, indexPath: NSIndexPath){
//        self.collectionView.dataSource = delegate
//        self.collectionView.delegate = delegate
//        self.collectionView.indexPath = indexPath
//        self.collectionView.tag = indexPath.row
//        self.collectionView.reloadData()
//    }

// MARK: - Collection view data source

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 5
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: CustomCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CustomCollectionViewCell
    //let tableViewCell = tableView!.cellForRowAtIndexPath(NSIndexPath(forItem: collectionView.tag, inSection: 0))
    var rect: CGRect?

    var label: UILabel = UILabel()
    label.font = UIFont(name: "HelveticaNeue-Light", size: 18.0)
    label.numberOfLines = 0
    label.lineBreakMode = NSLineBreakMode.ByWordWrapping
    label.textAlignment = .Center
    switch indexPath.row {
    case 0:
        cell.frame = CGRectMake(0, 0, size!.width * 0.2, size!.height)
        rect = cell.bounds
        label.frame = rect!
        label.text = event!.category
        break
    case 1:
        cell.frame = CGRectMake(size!.width * 0.2, 0, size!.width * 0.3, size!.height)
        rect = cell.bounds
        label.frame = rect!
        label.text = event!.event
        break
    case 2:
        cell.frame = CGRectMake(size!.width * 0.5, 0, size!.width * 0.26, size!.height)
        rect = cell.bounds
        label.frame = rect!
        label.text = event!.location
        break
    case 3:
        cell.frame = CGRectMake(size!.width * 0.76, 0, size!.width * 0.12, size!.height)
        rect = cell.bounds
        label.frame = rect!
        label.text = event!.date
        break
    case 4:
        cell.frame = CGRectMake(size!.width * 0.88, 0, size!.width * 0.12, size!.height)
        rect = cell.bounds
        label.frame = rect!
        label.text = event!.time
        break
    default:
        break
    }
    cell.addSubview(label)
    cell.layer.borderColor = UIColor.blackColor().CGColor
    cell.layer.borderWidth = 0.5

    return cell
}

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
}
}
class CustomCollectionViewCell: UICollectionViewCell {

override init(frame: CGRect) {
    super.init(frame: frame)

}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func awakeFromNib() {
    super.awakeFromNib()

}
}