所以我在表格 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/