【问题标题】:How to make Hozirontal UITableView with Custom UITableViewCell如何使用自定义 UITableViewCell 制作 Hozirontal UITableView
【发布时间】:2016-03-30 02:08:54
【问题描述】:

我是 Swift 的新手,我正在制作一个显示电影列表的视图,看起来像 appstore 主屏幕(水平 UITableView),所以我在自定义 UITableViewCell 时遇到了问题。单元格未显示在 UITableView 中。我不知道为什么?以及如何解决它!这是我的代码: 电影视图控制器:

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!
}

还有我的故事板:

还有我的 Sreen:

【问题讨论】:

标签: ios iphone swift uitableview collectionview


【解决方案1】:

您可以通过将您的 collectionView 的委托和数据源设置为情节提要中的 tableViewCell 的单元格类来实现。

【讨论】:

  • 我检查了 4 次,删除它并再次执行 3 次,但没有任何改变。
  • 您需要从表格视图委托方法的 cellForRowAtIndexPath 调用重新加载集合视图方法,即 [collectionView reloaDatd]。
  • 你能在teamviewer中帮我吗?
  • 目前无法使用 teamviewer。尝试在 cellForRowAtIndexPath func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell") 中使用此代码! CategoryRow cell.collectionView?.reloadData() return cell }
  • 已经设置了collection view的所有这些属性,1.scrollDirection = Horizo​​ntal 2. minimumLineSpacing 3. minimumInteritemSpacing
【解决方案2】:

抱歉误读了问题。

看起来您的 collectionView 可能没有将其委托设置为单元格。将您的 cellForRowAtIndexPath 更改为此,这将确保您至少生成水平单元格。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! CategoryRow
     cell.collectionView.delegate = cell
     return cell
}

【讨论】:

  • 我做它看起来像一个教程:thorntech.com/2015/08/… 但我不知道如何修复它
  • 他正在尝试在tableView中添加collectionView。这让它变得如此复杂。
  • 我添加了您的代码并出现错误:致命错误:在展开可选值时意外发现 nil
  • 听起来您的collectionView实际上并未连接到故事板中的CategoryRow tableViewCell。
  • 你能帮我吗?在 teamviewer 中?
【解决方案3】:

仔细检查您的标识符和插座(见下图)。

当您开始连接 API 调用以获取视频图像时,您可能想查看此 blog post,它是本教程的第 2 部分,但使用 AlamoFire 调用示例 API 并下载一些图像。

我注意到您也在使用滑出式菜单。平移手势可能与您水平滑动的集合视图单元格冲突。因此,您可能想改用屏幕边缘平移手势识别器。或者您可以查看 blog post 创建可自定义的交互式滑出菜单。

最后,要让您在不知道它是否可行的情况下集成所有这些教程需要很多。所以我继续创建了一个新的GitHub branch called evan,它结合了水平滚动条、API 调用和交互式滑出菜单。这是它的样子:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    相关资源
    最近更新 更多