ios - 使用自定义单元格创建多个部分

标签 ios swift uitableview

所以我在表格 View 中有 2 个自定义单元格。每个单元格都有自己独立的部分,“过滤”和“排序依据”

func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("FilterCell") as! FilterCell
        // Edit cell here
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("SortByCell") as! SortByCell
        // Edit cell here
        return cell
    }
}
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return 1
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2 }
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    switch section {
    case 0:
        return "Sort By"

    default:
        return "Filter"
    }
}

我试图用这些标题显示每个部分,但问题是这两个部分显示相同的自定义单元格。我的假设是我需要每个部分来检测通过标识符显示的自定义单元格,但我似乎无法弄清楚。我不确定我是否应该使用 switch 语句,但看起来似乎有道理

最佳答案

请伙计...

func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("SortByCell") as! SortByCell
        // Edit cell here
        return cell
    }
    let cell = tableView.dequeueReusableCellWithIdentifier("FilterCell") as! FilterCell
        // Edit cell here
    return cell
}

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

func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    switch section {
    case 0:
        return "Sort By"
    default:
        return "Filter"
    }
}

关于ios - 使用自定义单元格创建多个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36127752/

相关文章:

ios - 解析 - 在应用程序安装时调用云代码

iphone - 我如何使一个表格 View 指向具有另一个表格 View 的页面?

ios - 无法在 SpriteKit 中的坐标空间之间进行转换

objective-c - Objective-C : How to make background color of UITableView Consistent

ios - 如何拥有两个具有两种不同高度的独立单元格?

ios - 打开应用程序或更改场景时背景音乐继续播放

swift - 按时间戳对 Firebase 查询进行分页

swift - 我想将数据(UILabel)传递给另一个没有按钮的 View Controller 中的UILabel

ios - 如何根据 swift 3 中的 Collection View 高度动态增加 TableView 高度?

从 XIB 文件加载时,Swift 中的 iOS 键盘太宽太高