Ios 更改单个单元格的高度UITableView
我想要如下内容:Ios 更改单个单元格的高度UITableView,ios,swift,uitableview,Ios,Swift,Uitableview,我想要如下内容: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell if indexPa
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("NewsCell", forIndexPath: indexPath) as! UITableViewCell
if indexPath == 3{
cell.height = "50dp"
}
return cell
}
什么是替代方法或最简单的方法
编辑
我是否也可以指定章节号:即-
if sectionIndex == 5
我建议使用heightForRowAtIndexPath函数。TableView将调用此函数来确定特定indexpath的行高 示例代码:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == SECTION_INDEX {
return 60
} else {
return UITableViewAutomaticDimension
}
}
Swift 4:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == SECTION_INDEX {
return 60
} else {
return UITableViewAutomaticDimension
}
}
请稍作解释:
indexPath有两个属性:节和行。通过检查这两个属性,可以创建自己的控制流来确定每个单元格的高度
如果返回60,则表示希望单元格的高度为60点。如果返回UITableViewAutomaticDimension,则希望系统为您确定最佳高度(在这种情况下,最好为情节提要中的单元格设置自动布局)
我还建议你在iTunes U上学习斯坦福课程CS193p,这对你会有很大帮助:)最简单的方法是覆盖
高度为rowatinexpath
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let section = indexPath.section
let row = indexPath.row
if section == 0 && row == 2{
return 50.0
}
return 22.0
}
阅读表视图委托协议参考。这会强制您设置可能希望保持不变的单元格高度……我认为这就是Moritz没有选择答案的原因。
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
if indexPath.section == 5
{
if indexPath.row == 3
{
return 150.0
}
}
return 200.0
}