Ios 如何使用swift在UITableView节标题中添加图像?
我有一个4头球。我想在标题旁边添加一个图像 这些是我的头数组Ios 如何使用swift在UITableView节标题中添加图像?,ios,swift,uitableview,header,uiimageview,Ios,Swift,Uitableview,Header,Uiimageview,我有一个4头球。我想在标题旁边添加一个图像 这些是我的头数组 let sections: [String] = ["Cleaning","Computer Repair", "Electircity", "Painting", "Plumbing"] 如何使用swift在UITableView节标题中添加图像?您必须确认一些UITableViewDataSource和UITableViewDelegate方法 要声明节数,请调用func numberOfSections(在tableView:
let sections: [String] = ["Cleaning","Computer Repair", "Electircity", "Painting", "Plumbing"]
如何使用swift在UITableView节标题中添加图像?您必须确认一些UITableViewDataSource和UITableViewDelegate方法 要声明节数,请调用
func numberOfSections(在tableView:UITableView中)->Int
方法
要在节头中显示图像,请调用func tableView(\utableview:UITableView,viewForHeaderInSection:Int)->UIView?
方法
如果要设置节头的高度,请调用func tableView(\utableview:UITableView,estimatedHeightForHeaderInSection:Int)->CGFloat
方法
在viewForHeaderInSection
方法中,您可以设计标题。不要忘记设置自动布局约束
您可以创建一个UIImage数组来在节头中显示图像,如下所示
let sectionImages: [UIImage] = [picutre1, picture2, picture3, picture4]
这是完整的代码
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 80
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let imageView = UIImageView()
imageView.image = sectionImages[section]
let headerView = UIView()
headerView.backgroundColor = .white
headerView.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.centerXAnchor.constraint(equalTo: headerView.centerXAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: headerView.centerYAnchor).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 60).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 60).isActive = true
return headerView
}
好的,如果我创建这样一个数组,““”sectionImages:[[UIImages]]=[[picture1]、[picture2]、[picture3]、[picture4]、[picture5]]我如何添加标题这个数组是UIImages的数组?数组应该是这样的
sectionImages:[UIImages]=[picture1,picture2,picture3,picture4]
很抱歉我写错了。我的问题是如何将此部分图像用于标题图像