Ios 如何在静态TableViewCell中显示UICollectionView?
我想在staticIos 如何在静态TableViewCell中显示UICollectionView?,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我想在statictableview单元格内显示UICollectionView 我有一个静态的TableViewCell,其中包含CollectionView。问题是,在我看来,代码看起来不错,但当我启动程序时,TableViewCell不会显示CollectionView。我有点困惑,如果你能帮助我,我会很高兴的。谢谢 TableViewController类: ActionCell: 另外,我必须注意到,在故事板中我提到了所有类和重用ID,如果您将带有静态单元格的“UITableView”
tableview单元格内显示UICollectionView
我有一个静态的TableViewCell
,其中包含CollectionView
。问题是,在我看来,代码看起来不错,但当我启动程序时,TableViewCell
不会显示CollectionView。我有点困惑,如果你能帮助我,我会很高兴的。谢谢
TableViewController类:
ActionCell:
另外,我必须注意到,在故事板中我提到了所有类和重用ID,如果您将带有静态单元格的“UITableView”用作内容,则不需要实现“tableView”(“tableView:UITableView,cellForRowAt indexPath:indexPath)”,所有内容都可以在故事板中定义
以下是UITableViewController在情节提要中的外观,它使用自定义单元格(TableCell、CollectionCell)作为表和集合视图,并具有“UITableViewCell”
充当包含“UICollectionView”的数据源:
下面是UITableViewController的实现:
类ViewController:UITableViewController{
}
类TableCell:UITableViewCell{
}
类CollectionCell:UICollectionViewCell{
}
扩展TableCell:UICollectionViewDataSource{
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
let cell=collectionView.dequeueReusableCell(withReuseIdentifier:“collectionCell”,for:indexath)
返回单元
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回1
}
}
您是否设置了collectionview的委托
和数据源
?是的,我这样做了…我试图使用您的解决方案,但我遇到了以下错误:'-[UITableViewCell collectionview:numberOfItemsInSection::]:无法识别的选择器发送到实例0x106057200'。为这些错误感到抱歉,我感到很惭愧。我的代码在这里:您是否将数据源设置为自定义的“ActionCell”?这是我发现错误的示例项目!谢谢。我真是个白痴。祝你新年快乐
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var Cell = tableView.dequeueReusableCell(withIdentifier: "Action")
switch indexPath.section {
case 0:
tableView.register(ScheduleCell.self, forCellReuseIdentifier: "Schedule")
Cell = tableView.dequeueReusableCell(withIdentifier: "Schedule") as! ScheduleCell
case 1:
return Cell!
case 2:
tableView.register(MarksCell.self, forCellReuseIdentifier: "Marks")
Cell = tableView.dequeueReusableCell(withIdentifier: "Marks") as! MarksCell
default:
Cell = tableView.dequeueReusableCell(withIdentifier: "Action") as! ActionCell
}
return Cell!
}
extension ActionCell: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ActionCell", for: indexPath) as! ActionsCollectionViewCell
var Labels = ["РАСПИСАНИЕ", "ДОМАШНИЕ ЗАДАНИЯ", "ОЦЕНКИ"]
var Icons = ["Safari", "DZ", "Rating"]
var Colors = [UIColor.init(red: 26/255, green: 196/255, blue: 234/255, alpha: 1),
UIColor.init(red: 251/255, green: 143/255, blue: 25/255, alpha: 1),
UIColor.init(red: 251/255, green: 25/255, blue: 118/255, alpha: 1)]
cell.Title.text = Labels[indexPath.item]
cell.Icon.image = UIImage.init(named: Icons[indexPath.item])
cell.Button.backgroundColor = Colors[indexPath.item]
return cell
}
}