单击时如何更改单个单元格的高度? [英] how to change the height of a single cell when clicked?

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

问题描述

单击时我必须调整tableView的一行。我怎么能这样做?有人可以帮帮我吗?



我的视图控制器类:

  class DayViewController:UIViewController,UITableViewDataSource,UITableViewDelegate {

@IBOutlet var daysWorkPointTable:UITableView

override func viewDidLoad(){
super.viewDidLoad()

var nipName = UINib(nibName:daysWorkPointsCell,bundle:nil)

self.daysWorkPointTable.registerNib(nipName,forCellReuseIdentifier:daysWorkCell)
}

func tableView(tableView:UITableView!,numberOfRowsInSection section:Int) - > Int {
return 1
}

func tableView(tableView:UITableView!,heightForRowAtIndexPath indexPath:NSIndexPath) - > CGFloat {
返回75
}

func tableView(tableView:UITableView!,cellForRowAtIndexPath indexPath:NSIndexPath!) - > UITableViewCell的! {
var cell = tableView.dequeueReusableCellWithIdentifier(daysWorkCell,forIndexPath:indexPath)as daysWorkPointsCell

return cell
}

func tableView(tableView: UITableView!,didSelectRowAtIndexPath indexPath:NSIndexPath!){

}
}


  var selectedCellIndexPath:NSIndexPath? 

它应该是可选的,因为您可以不选择单元格。
Next允许声明选定和未选定状态的高度(将值更改为您想要的任何值):

  let selectedCellHeight: CGFloat = 88.0 
let unselectedCellHeight:CGFloat = 44.0

现在你必须实现 tableView(_:,heightForRowAtIndexPath:)

 覆盖func tableView(tableView:UITableView ,heightForRowAtIndexPath indexPath:NSIndexPath) - > CGFloat {
if selectedCellIndexPath == indexPath {
return selectedCellHeight
}
return unselectedCellHeight
}

现在在你的 tableView(_:,didSelectRowAtIndexPath:)方法中,你必须检查选定的行或未选择的行已被挖掘:

 覆盖func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){
if selectedCellIndexPath!= nil&& selectedCellIndexPath == indexPath {
selectedCellIndexPath = nil
} else {
selectedCellIndexPath = indexPath
}

tableView.beginUpdates()
tableView。 endUpdates()

if selectedCellIndexPath!= nil {
//这确保一旦扩展
tableView.scrollToRowAtIndexPath(indexPath,atScrollPosition:.None,animated),单元格完全可见:true)
}
}

beginUpdates () endUpdates()调用为您提供动画高度更改。



如果你想改变高度变化动画的持续时间,可以包装 beginUpdates() endUpdates()在动画块中调用 UIView.animationWithDuration(...)并将其设置为您想要的任何值。 / p>

您可以查看此示例项目这段代码在行动中。


I have to resize a single row of my tableView when clicked. How I can do this? Anybody could help me?

My view controller class:

class DayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var daysWorkPointTable: UITableView

    override func viewDidLoad() {
        super.viewDidLoad()

        var nipName = UINib(nibName: "daysWorkPointsCell", bundle: nil)

        self.daysWorkPointTable.registerNib(nipName, forCellReuseIdentifier: "daysWorkCell")
    }

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath) -> CGFloat {
        return 75
    }

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell = tableView.dequeueReusableCellWithIdentifier("daysWorkCell", forIndexPath: indexPath) as daysWorkPointsCell

        return cell
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    }
}

解决方案

First you have to keep track of the indexPath of currently selected cell in a property:

var selectedCellIndexPath: NSIndexPath?

It should be an optional, because you can have no cell selected. Next lets declare heights for selected and unselected state (change the values to whatever you want):

let selectedCellHeight: CGFloat = 88.0
let unselectedCellHeight: CGFloat = 44.0

Now you have to implement tableView(_:, heightForRowAtIndexPath:):

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if selectedCellIndexPath == indexPath {
        return selectedCellHeight
    }
    return unselectedCellHeight
}

Now in your tableView(_:, didSelectRowAtIndexPath:) method you have to check wether the selected row or an unselected row has been tapped:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath {
        selectedCellIndexPath = nil
    } else {
        selectedCellIndexPath = indexPath
    }

    tableView.beginUpdates()
    tableView.endUpdates()

    if selectedCellIndexPath != nil {
        // This ensures, that the cell is fully visible once expanded
        tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
    }
}

The beginUpdates() and endUpdates() calls are giving you an animated height change.

If you want to change the duration of the height change animation you can wrap the beginUpdates() and endUpdates() calls in an animation block UIView.animationWithDuration(...) and set it to whatever value you want.

You can check out this sample project which demonstrates this code in action.

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

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