Ios tableView:具有不同样式的多个分区

Ios tableView:具有不同样式的多个分区,ios,swift,uitableview,header,sections,Ios,Swift,Uitableview,Header,Sections,我试图在Xcode中创建一个表,我希望该表中有3个部分,每个部分都有不同的标题 我只想粘贴上一节的标题,当它在顶部滚动时,而不是其他节 有什么办法吗?请建议我。1-是的,你可以这样做。在UITableViewDelegate的numberOfSections方法中,指定所需的节数。在UITableviewDelegate的viewForHeaderInSection方法中,为每个节提供自定义视图 func numberOfSections(in tableView: UITableView) -

我试图在Xcode中创建一个表,我希望该表中有3个部分,每个部分都有不同的标题

我只想粘贴上一节的标题,当它在顶部滚动时,而不是其他节


有什么办法吗?请建议我。

1-是的,你可以这样做。在UITableViewDelegate的
numberOfSections
方法中,指定所需的节数。在UITableviewDelegate的
viewForHeaderInSection
方法中,为每个节提供自定义视图

func numberOfSections(in tableView: UITableView) -> Int {
    return 3
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    // Make custom header view for each section
    if section == 0 {
        let header1 = UIView()
        return header1

    } else if section == 1 {
        let header2 = UIView()
        return header2

    } else if section == 2 {
        let header3 = UIView()
        return header3
    }
}

不,你不能那样做。UITableView不允许您指定哪个特定标题应粘贴到顶部,哪个不应粘贴到顶部。

您可以将UITableView样式设置为普通。滚动时,它将粘贴所有节标题


我认为不可能粘贴特定的节标题。

您可以通过在前两个节中使用自定义单元格作为标题来实现相同的行为。然后删除这些文件的实际标题

例如:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if (indexPath.section == 0 || indexPath.section == 1) && indexPath.row == 0 {
        return HeaderCell()
    }

    // return your desired cells (make sure to handle the number of
    // rows properly as there is effectively 1 more than normal in
    // in the first two sections now)
}
您还需要调整前两个部分的标题:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 2 { return YourHeaderView() }

    return nil
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if section == 2 { return yourHeight }

    return 0
}
此外,在
numberOfRows
委托函数中,您应该确保添加1(这将是
HeaderCell