我是 swift 的新手,我正在制作一个显示电影列表的 View ,看起来像应用商店主屏幕(水平 UITableView),所以我在自定义 UITableViewCell 时遇到了问题。单元格未显示在 UITableView 中。我不知道为什么?以及如何修复它!这是我的代码: 电影 View Controller :
class movieViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var mCollection: UICollectionView!
@IBOutlet weak var mTable: UITableView!
var categories = ["Phim Đang Hot", "Phim Lẻ", "Phim Bộ", "Phim Hài", "Phim Kinh Dị"]
@IBOutlet weak var btnMenu: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
btnMenu.target = self.revealViewController()
btnMenu.action = Selector("revealToggle:")
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
self.revealViewController().rearViewRevealWidth = 200
self.mTable.registerClass(CategoryRow.self, forCellReuseIdentifier: "Cell")
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return categories[section]
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return categories.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CategoryRow
return cell
}
}
和 CategoryRow.swift:
class CategoryRow : UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
}
extension CategoryRow : UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 12
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("videoCell", forIndexPath: indexPath) as! VideoCell
return cell
}}
extension CategoryRow : UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let itemsPerRow:CGFloat = 4
let hardCodedPadding:CGFloat = 5
let itemWidth = (collectionView.bounds.width / itemsPerRow) - hardCodedPadding
let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding)
return CGSize(width: itemWidth, height: itemHeight)
}
这是我的 VideoCell:
class VideoCell : UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
}
和我的 Storyboard:
最佳答案
您可以通过将 collectionView 的委托(delegate)和数据源设置为 Storyboard 中的 tableViewCell 的单元格类来实现。
关于ios - 如何使用自定义 UITableViewCell 制作 Hozirontal UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36298396/