ios - 如何在 tableview 中设置类似 collectionview 的东西?

标签 ios swift

这是我想要实现的目标的直观示例。

sample of the design

我最初的想法是使用带有自定义单元格的 UICollectionView,但这效果不是很好,在 CollectionView 中尝试 CollectionView 效果更糟。

所以我尝试在tableview中做一个collectionview,这种方法有效,但是tableviewcell的动态高度被证明是一个巨大的问题,这对我来说非常难以解决,因为我才刚刚开始学习swift和iOS开发。请帮助:(

编辑: 请注意,“Shopping Mall 1”部分是一个粘性标题。与此类似(绿松石标题):

gif

学分:https://github.com/jamztang/CSStickyHeaderFlowLayout

但我用的是 https://github.com/petec-blog/CollectionViewStickyHeaders作为一个例子,因为它更清晰,更接近我所需要的。

最佳答案

您可以使用下面的代码来了解这个想法(Xcode 7.2 (7C68)) 删除Info.plist中的storyboard、launchscreen、Clean properties Main story board和Launchscreen,将AppDelegate.swift替换为以下内容

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window!.rootViewController = CollectionViewInTableView()
        self.window!.makeKeyAndVisible()
        return true
    }
}

class CollectionViewInTableView: UIViewController, UITableViewDelegate, UITableViewDataSource {
    let table = UITableView()
    override func viewDidLoad() {
        table.delegate = self
        table.dataSource = self
        self.view.backgroundColor = UIColor.whiteColor()
        table.registerClass(Cell.self, forCellReuseIdentifier: Cell.self.description())
        self.view.addSubviewWithConstraints(["v" : table], constraints: ["H:|[v]|", "V:|-50-[v]|"])
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {return 2}
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {return 2}
    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {return "Shopping Moll \(section)"}

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCellWithIdentifier(Cell.self.description(), forIndexPath: indexPath) as! Cell
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {return 200}
}


class Cell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    var collectionView: UICollectionView!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCellSelectionStyle.None


        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 75, height: 60)
        layout.headerReferenceSize = CGSize(width: contentView.frame.width, height: 20)

        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: 200), collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate = self

        collectionView.registerClass(Item.self, forCellWithReuseIdentifier: Item.self.description())
        collectionView.registerClass(SectionTitle.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: SectionTitle.self.description())
        self.backgroundColor = UIColor.yellowColor()
        collectionView.backgroundColor = UIColor.whiteColor()
        self.contentView.addSubview(collectionView)
    }

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        return collectionView.dequeueReusableCellWithReuseIdentifier(Item.self.description(), forIndexPath: indexPath) as! Item
    }
    required init?(coder: NSCoder) {super.init(coder: coder)}

    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        switch kind {
        case UICollectionElementKindSectionHeader:
            let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: SectionTitle.self.description(), forIndexPath: indexPath) as! SectionTitle
            headerView.label.text = "Shop \(indexPath.section)"
            return headerView
        default: assert(false, "Unexpected element kind")
        }
    }

}

class SectionTitle: UICollectionReusableView {
    var label: UILabel!
    override init(frame: CGRect) {
        super.init(frame:CGRectZero)
        label = UILabel()
        label.text = "text"
        self.addSubviewWithConstraints(["v" : label], constraints: ["H:|-10-[v]|","V:|[v]|"])
    }

    required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}
}


class Item: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        let v = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        v.backgroundColor = UIColor.blueColor()
        contentView.addSubview(v)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension UIView {
    func addSubviewWithConstraints(views: [String : AnyObject], constraints: Array<String>) -> [String : AnyObject] {
        for (_, view) in views {
            self.addSubview(view as! UIView)
            (view as! UIView).translatesAutoresizingMaskIntoConstraints = false
        }
        for var i = 0; i < constraints.count; i++ {self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(constraints[i], options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))}
        return views
    }
}

关于ios - 如何在 tableview 中设置类似 collectionview 的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35058318/

相关文章:

ios - 以编程方式在 UIView 上添加 UITextField Swift

ios - XCode 无法启动应用程序 "The operation could not be completed (FBSOpenApplicaionpplicaionErrorDomain error 3.)"

ios - 某些属性字符串在绘制时会崩溃

iphone - 提交应用到苹果应用商店后如何更改 "Version Release Control"

php - 在带有 AFnetworking 的 iOS 应用程序上使用 cookie

swift - 无法将值类型 ""转换为预期参数 'UInt64'

ios - 为什么 YouTube 会干扰 iPad 悬停下拉菜单?

ios - 如何从通用链接获取 url?

swift - 如何将Realm中某个类的所有对象获取到一个数组中

ios - 用户默认值,附加用户每次提交按钮时输入的数字