ios - 如何使用自定义 UITableViewCell 制作 Hozirontal UITableView

标签 ios iphone swift uitableview collectionview

我是 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:

enter image description here

还有我的Sreen: enter image description here

最佳答案

您可以通过将 collectionView 的委托(delegate)和数据源设置为 Storyboard 中的 tableViewCell 的单元格类来实现。

关于ios - 如何使用自定义 UITableViewCell 制作 Hozirontal UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36298396/

相关文章:

ios - UIView 动画 shadowPath 不遵循

iphone - 在调用另一个方法时阻止 AppDelegate 加载 ViewController

ios - Swift 中的 Adob​​e Creative SDK

ios - 如何让语音合成器一个接一个地说出一堆话语?

swift - 使用 Swift 4 Codable PropertyListDecoder() 解码 PropertyList

ios - FCM 为 IOS 通知抛出无效参数

ios - 如何设置子 TableView 中选定行的背景颜色?

ios - 如何在加载tableview之前执行API方法?

iphone - iPad 模态视图不旋转

ios - Xcode 6.1.1 为通用构建启动图像