当iOS8没有足够的行填充tableView时,所有现有的解决方案都不适用于我,因为iOS会在这种情况下自动调整插入。 (当有足够的行时现有的答案是好的)
在这个问题上浪费了6小时后,我终于得到了这个解决方案。
总之,如果没有足够的单元格,您需要将空单元格插入到tableView中,因此tableView的内容大小足够大,iOS不会为您调整嵌入。
下面是我在Swift中做的:
1。)声明变量minimumCellNum
作为一类属性
var minimumCellNum: Int?
2.)计算minimumCellNum
和viewWillAppear
let screenHeight = Int(UIScreen.mainScreen().bounds.height)
// 101 = Height of Status Bar(20) + Height of Navigation Bar(44) + Height of Tab Bar(49)
// you may need to subtract the height of other custom views from the screenHeight. For example, the height of your section headers.
self.minimumCellNum = (screenHeight - 103 - heightOfOtherCustomView)/heightOfYourCell
self.tableView.contentOffset = CGPointMake(0, 44)
3.)在tableView(tableView: UITableView, numberOfRowsInSection section: Int))
let numOfYourRows = YOUR LOGIC
if numOfYourRows > minimumCellNum {
return numOfYourRows
} else {
return minimumCellNum!
}
4.设置tableView.contentOffset
)注册一个空格,其selection
属性为None
,在故事板上和tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
if indexPath.row < numOfYourRows {
return YOUR CUSTOM CELL
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("EmptyCell", forIndexPath: indexPath) as! UITableViewCell
return cell
}
在tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
if tableView == self.tableView {
if numOfYourRows < (indexPath.row + 1) {
return
}
YOUR LOGIC OF SELECTING A CELL
}
这不是一个完美的解决方案,但它是唯一的解决方法是真的对我的作品在iOS 8.我想知道如果有5)一个整洁的解决方案。
此作品 - 谢谢! – Tim 2009-07-06 23:45:33
其实,我得到的问题是,如果没有足够的行来填充屏幕,表格视图将不会滚动 - 在一行或两行表格中,此代码不会执行任何操作。那是预期的行为?我能以某种方式避开它吗? – Tim 2009-07-10 19:54:22
这样的声音通常是有意的(如果适合屏幕显示所有内容)。然而邮件应用程序似乎可以滚动搜索栏,即使没有行存在。不幸的是,我无法在我自己的代码中重现这一点。 – Zargony 2009-07-14 21:51:21