首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将动态原型单元链接到不同的UITableViewControllers?

如何将动态原型单元链接到不同的UITableViewControllers?
EN

Stack Overflow用户
提问于 2015-03-15 18:21:23
回答 2查看 451关注 0票数 0

对不起,我对code & Swift是个新手。

无法发布图像,但我需要将我的自定义UITableViewController 7动态原型细胞链接到7个不同的UItableviewcontrollers。到目前为止,我尝试将单元分割到另一个UITableViewController,但其余单元也链接到相同的位置。假设我想将一个快餐类别链接到一个UITableViewController中的快餐店列表,其他所有类别也链接到同一个UITableViewController。

我不太确定该怎么做。

我的主控制器代码如下:

代码语言:javascript
复制
class CategoriesTableViewController: UITableViewController {

    var category: [Categories] = categoriesData

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return category.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("CategoriesCell", forIndexPath: indexPath)as CategoriesCell

                let categories = category[indexPath.row] as Categories
                cell.textLabel?.text = categories.category
                if let categoryLabel = cell.viewWithTag(100) as? UILabel {
                    categoryLabel.text = categories.category
                }
                return cell
            }

这是我的categories.swift文件:

代码语言:javascript
复制
class Categories: NSObject {
    var category: String
    init(category: String) {
        self.category = category
        super.init()
    }
}

categories data.swift

代码语言:javascript
复制
let categoriesData = [Categories(category:"Restaurants/Cafe"), Categories(category:"Fine Dining"), Categories(category:"Catering"),Categories(category:"Buffet"), Categories(category:"Food Court/Hawker Centre"), Categories(category:"Fast Food"), Categories(category:"Others")]

my categories cell.swift shows the following:

class CategoriesCell: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel!
}
EN

回答 2

Stack Overflow用户

发布于 2015-03-15 18:26:53

也许可以张贴图片,这样我们就可以看到你为什么要走那条路线。

我想您希望表格视图的各个部分显示不同的内容?

我建议:

  1. 将表视图拆分为所需数量的部分(每个部分可以有给定的行数)
  2. 在中创建一个UITableViewController类

override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { }

根据区段和行号设置内容

票数 0
EN

Stack Overflow用户

发布于 2015-03-15 19:30:49

试试这个:

  1. 创建7个不同的原型单元格。
  2. 更改每个原型单元格的重用标识符,以便将其中一个原型单元格与带段的不同ViewController相匹配。
  3. cellForRowAtIndexPath中,使用该行的类别获取正确的原型单元格:

让categories = Categories作为Categories让CategoriesCell = categoryindexPath.row forIndexPath: indexPath)作为Categories

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29059541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档