我试图在 Swift 中设置静态 UITableViewCell IBOutlet 的高度。这可能吗?我已经找到了如何更改表格 View 中所有单元格的答案,但我只想更改此单元格并保留其余单元格,因为它们在 IB 中的大小相同。
我已经尝试过这个:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 1 && indexPath.row == 1 {
return CGFloat(screenSize.width)
}
else{
return UITableViewAutomaticDimension
}
}
最佳答案
要在静态 TableViewCell 上动态设置高度,您必须在 heightForRowAtIndexPath
中进行设置。以下是更改第一部分中的第二行的实现:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 0 && indexPath.row == 1 {
// Here return a number for the height you desire
return CGFloat(100)
}
else {
return UITableViewAutomaticDimension
}
}
以下是一些可能与您的情况相关的提示。如果它们是故意在您的代码中完成的,请忽略它们。
单元格的高度设置为屏幕的宽度,可能值得检查这是否是有意的。另请记住,部分和行都从索引 0 开始,而不是从索引 1 开始。
关于swift - 仅设置一个静态 UITableViewCell 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31208707/