Ios 如何将自定义单元格设置为UITableView的页眉或页脚

Ios 如何将自定义单元格设置为UITableView的页眉或页脚,ios,swift,uitableview,custom-cell,uitableviewsectionheader,Ios,Swift,Uitableview,Custom Cell,Uitableviewsectionheader,我使用的是Xib文件而不是故事板。我用一些虚拟数据创建了一个表视图。工作正常,现在我已经创建了一个带有一些标签和文本字段的自定义单元格。如何将其用作UITableView的页眉或页脚?您可以通过编程方式创建自定义视图。我尝试了以下代码。试试看 override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { let customview:UIVi

我使用的是Xib文件而不是故事板。我用一些虚拟数据创建了一个表视图。工作正常,现在我已经创建了一个带有一些标签和文本字段的自定义单元格。如何将其用作UITableView的页眉或页脚?

您可以通过编程方式创建自定义视图。我尝试了以下代码。试试看

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let customview:UIView = UIView();
        let label = UILabel();
        label.text = "ur custom data"
        /*
            You can add any number of subviews you want here
            And don't forget to add it to your customview.
         */
        customview.addSubview(label)
        return customview;
    }

同样,您也可以对标题执行相同的操作。

这是添加TableView标题的方法: 您可以使用任何控件,如UIButton、UIImageView等

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionView = UIView()
        sectionView.frame = CGRectMake(x,y,height,width) // For Set the Frame 
        sectionView.backgroundColor = UIColor.redColor()

        return sectionView
    }
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let sectionView = UIView()
        sectionView.frame = CGRectMake(x,y,height,width) // For Set the Frame 
        sectionView.backgroundColor = UIColor.redColor()

        return sectionView
    }
如上所述,您可以设置节和的页脚 您可以添加页眉和页脚的大小

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 20.0
}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 20.0
}
Swift 4.0

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                let sectionView = UIView()
                sectionView.frame = CGRect(x: x, y: y, width: width, height: height)// For Set the Frame
                sectionView.backgroundColor = UIColor.red

                return sectionView
    }

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
                let sectionView = UIView()
                sectionView.frame = CGRect(x: x, y: y, width: width, height: height)// For Set the Frame
                sectionView.backgroundColor = UIColor.red

                return sectionView
}

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20.0
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 20.0
    }

答案其实很简单。 在
viewForHeaderInSection()
中,像在
cellforrowatinexpath()
中一样创建单元格对象并返回它

下面是示例代码

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as! YourTableViewCell
    return cell
}
可以将收割台的高度设置为:

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 80
} 
Swift 4.2的更新

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier") as! YourTableViewCell
    return cell
}


您不能将UITableView单元格用作页眉或页脚,因为您需要创建一个视图(xib),并且可以将itcell用作页眉?tableHeaderView是一种视图类型,而不是单元。如何使用和Xib创建UiView?当我创建一个新的CoCoatTouch文件并使其成为UIView的子类时,“还创建Xib文件”选项不可选中。在这里,我创建了一个包含imageView的cstom视图。当我尝试在简单视图中使用此方法时,使用nackground颜色是可行的,但不适用于我的自定义视图添加Tableview单元格不是一个好的解决方案
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 80
}