更改单个单元格UITableView的高度 [英] Changing height of individual cell UITableView

查看:94
本文介绍了更改单个单元格UITableView的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要如下内容:

  func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCell {

让cell = tableView.dequeueReusableCellWithIdentifier(NewsCell,forIndexPath:indexPath)为! UITableViewCell

如果indexPath == 3 {

cell.height =50dp

}

return cell

}

什么是替代或最简单的方法?



编辑



我是否也可以指定章节编号:ie - 如果sectionIndex == 5


解决方案

我建议使用 heightForRowAtIndexPath 函数。 TableView将调用此函数来确定特定索引路径的行高。



示例代码:

  func tableView(tableView:UITableView,heightForRowAtIndexPath indexPath:NSIndexPath) - > CGFloat {
如果indexPath.section == SECTION_INDEX {
返回60
}否则{
返回UITableViewAutomaticDimension
}
}

Swift 4:

  func tableView(_ tableView:UITableView,heightForRowAt indexPath:IndexPath) - > CGFloat {
如果indexPath.section == SECTION_INDEX {
返回60
}否则{
返回UITableViewAutomaticDimension
}
}

一点解释:
indexPath有两个属性:section和row。通过检查这两个属性,您可以创建自己的控制流来确定每个单元格的高度。



如果返回60,则表示您希望单元格的高度为60分。如果您返回UITableViewAutomaticDimension,您希望系统为您决定最佳高度(在这种情况下,您最好为故事板中的单元格设置autolayout)。



I还建议你在iTunes U上学习斯坦福课程CS193p,这对你有很大的帮助:)。


I would like something like the following:

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

}

what is the alternative or the easiest way to go about this?

EDIT

Is it possible for me to also specify section number: i.e-

if sectionIndex == 5

解决方案

I suggest use the heightForRowAtIndexPath function. TableView will call this function to determine the row height for a specific indexpath.

Sample code:

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
    }
}

A little explain: indexPath have two properties: section and row. By checking these two properties, you can make your own control flow to determine the height for each cell.

If you return 60, it means you want the cell's height to be 60 points. If you return UITableViewAutomaticDimension, you want the system to decide the best height for you (in this case, you'd better set autolayout for the cell in storyboard).

I also suggest you take the stanford course CS193p on iTunes U, it would be of great help to you :)

这篇关于更改单个单元格UITableView的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆