我有一个包含16个部分的静态表视图。当我尝试修改heightForRowAtIndexPath中的高度时,"部分" indexPath返回一个从0到3的数字,而不是预期的0到15.下面是我的代码:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
print("Section: \(indexPath.section), Row: \(indexPath.row)")
if indexPath.row == 0 {
return heightArray![indexPath.section]
} else {
return UITableViewAutomaticDimension
}
}
它打印以下内容:
Section: 0, Row: 0
Section: 0, Row: 1
Section: 0, Row: 2
Section: 1, Row: 0
Section: 1, Row: 1
Section: 2, Row: 0
Section: 2, Row: 1
Section: 2, Row: 2
Section: 3, Row: 0
Section: 3, Row: 1
Section: 0, Row: 0
Section: 0, Row: 1
Section: 0, Row: 2
Section: 1, Row: 0
Section: 1, Row: 1
Section: 2, Row: 0
Section: 2, Row: 1
Section: 2, Row: 2
Section: 3, Row: 0
发生了什么?
修改
这是我向上和向下滚动时发生的事情......
修改
这里出现的内容不正确。这是第4节。它没有格式化我希望它在heightForRowAtIndexPath中格式化的方式。请注意,M03格式正常(该部分的单元格#0是正确的高度)。 M04和之后的格式不正确。我在" heightArray"中设置值。在viewDidAppear中,我试图在tableView.reloadData()中使用这些高度。
修改 这是我的ViewDidAppear方法:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.canDisplayBannerAds = true
textViewArray = [m1DF,m2DF,m3DF,m4DF,m5DF,m6DF,m7DF,m8DF,m9DF,m10DF,m11DF,m12DF,m13DF,m14DF,m15DF,m16DF]
print("*****1*****\n\n\n\n\n \(m1DF.bounds.height), \(m1DF.frame.height)")
for (index, tv) in textViewArray!.enumerate() {
let fixedWidth = tv.frame.size.width
tv.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
let newSize = tv.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
var newFrame = tv.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
tv.frame = newFrame;
heightArray[index] = newSize.height
print("Row \(index) has height \(newSize.height)")
}
print(heightArray)
for (index, _) in (heightArray.enumerate()) {
heightArray[index] = textViewArray![index].frame.height
}
print(heightArray)
tableView.reloadData()
}
答案 0 :(得分:1)
此方法必须返回16:
func numberOfSections(in tableView: UITableView) -> Int {
return someValue // as you note you have 16 sections, this number has to have 16 in its value
}