我正在创建一个可扩展的tableview。我为每个部分添加了部分和行。为样式部分创建了一个xib文件,它可以在iphone 5s中正常工作。但是当我切换到iphone 8s时,我的部分仍然是iphone 5s的大小,宽度不会改为tableview。我设置了约束,我的一些代码如下:
override func viewDidLoad() {
super.viewDidLoad()
self.homeMenuTableView.backgroundColor = UIColor(red: 0.0196, green: 0.1059, blue: 0.1647, alpha: 1.0)
let px = 1 / UIScreen.main.scale
let frame = CGRect(x:0,y:0,width: self.homeMenuTableView.frame.size.width,height: px)
let line = UIView(frame: frame)
self.homeMenuTableView.tableHeaderView = line
line.backgroundColor = self.homeMenuTableView.separatorColor
homeMenuTableView.tableFooterView = UIView()
homeMenuTableView.dataSource=self
homeMenuTableView.delegate=self
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = ExpandableHeaderView()
header.customInit(title: sections[section].menuCellLabel, section: section, delegate: self)
return header
}
func customInit(title: String, section: Int, delegate: ExpandableHeaderViewDelegate) {
let cell = Bundle.main.loadNibNamed("MenuTableViewCell", owner: self, options: nil)?.first as! MenuTableViewCell
let menucell = DataService.instance.getMenuCells()[section]
cell.updateViews(menuCell: menucell)
self.addSubview(cell)
self.section = section
self.delegate = delegate
}
答案 0 :(得分:0)
您正在从xib加载单元格,但您没有为其分配框架。由于它需要XIB中的单元格宽度
func customInit(title: String, section: Int, delegate: ExpandableHeaderViewDelegate) {
let cell = Bundle.main.loadNibNamed("MenuTableViewCell", owner: self, options: nil)?.first as! MenuTableViewCell
let menucell = DataService.instance.getMenuCells()[section]
cell.updateViews(menuCell: menucell)
//Add this line in your code width issue will resolve
cell.frame.size.width = tableView.frame.width
self.addSubview(cell)
self.section = section
self.delegate = delegate
}