UITableView:如何在单击按钮时动态更改单元格高度?迅速 [英] UITableView: How to change cell height dynamically when a button is clicked in it? Swift

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

问题描述

我可以在

此外,再次单击它时,它应该变回原始大小.我对ObjectiveC不熟悉,因此请在Swift中帮助我.预先感谢一堆!

这是我到目前为止所拥有的:

 //建议的变量声明var shouldCellBeExpanded:Bool = falsevar indexOfExpendedCell:NSInteger = -1 

现在在ViewController中.注意:TableViewCell是我的自定义单元格的名称.

 //在ViewController内部func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell {如果让cell:TableViewCell = TableViewCell(){cell.stopWatch = stopWatchBlocks [indexPath.row]cell.expandButton.tag = indexPath.row//将动作添加到展开按钮cell.expandButton.addTarget(self,action:"expandButtonAction1:",forControlEvents:UIControlEvents.TouchUpInside)返回单元}} 

现在,按钮操作方法:

  func expandButtonAction1(button:UIButton){button.selected =!button.selected如果button.selected {indexOfExpendedCell = button.tagshouldCellBeExpanded = trueself.TableView.beginUpdates()self.TableView.reloadRowsAtIndexPaths([NSIndexPath(forItem:indexOfExpendedCell,inSection:0)],withRowAnimation:.Automatic)self.TableView.endUpdates()button.setTitle("x",forState:UIControlState.Selected)}否则,如果!button.selected {indexOfExpendedCell = button.tagshouldCellBeExpanded = falseself.TableView.beginUpdates()self.TableView.reloadRowsAtIndexPaths([NSIndexPath(forItem:indexOfExpendedCell,inSection:0)],withRowAnimation:.Automatic)self.TableView.endUpdates()button.setTitle("+",forState:UIControlState.Normal)}} 

最后还有HeightForRowAtIndexPath

  func tableView(tableView:UITableView,heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat {如果应该CellBeExpanded&&indexPath.row == indexOfExpendedCell {回报166.0}否则{返回91.0}} 

我想我丢失了一些东西,因为单击一次该单元格确实会扩展,但是它不会缩小"到91!

我在做什么错了?

尽量不要将所选标签用作此类单元格状态的切换.选定一个单元格后,再次点击它不会取消选择.相反,您可以只使用shouldCellBeExpanded标志:

  func expandButtonAction1(button:UIButton){shouldCellBeExpanded =!shouldCellBeExpandedindexOfExpendedCell = button.tag如果shouldCellBeExpanded {self.TableView.beginUpdates()self.TableView.endUpdates()button.setTitle("x",forState:UIControlState.Normal)}别的 {self.TableView.beginUpdates()self.TableView.endUpdates()button.setTitle("+",forState:UIControlState.Normal)}} 

另外,根据我的经验,不需要reloadRowsAtIndexPath方法.除非需要自定义动画,否则单独使用beginUpdates()和endUpdates()应该可以解决问题.

I can tell how to do this in objective-c from here, but how can I convert that to swift?

I have a UITableView with custom TableViewCells that has a UIButton called "expandButton". I am trying to figure out how to change the height of that particular cell when the expandButton for that cell is clicked.

Also, when it is clicked again, it should change back to original size. I am not familiar with ObjectiveC so please help me with this in Swift. Thanks a bunch in advance!

Here is what I have so far:

    //Declaration of variables as suggested
        var shouldCellBeExpanded:Bool = false
        var indexOfExpendedCell:NSInteger = -1

Now inside the ViewController. Note: TableViewCell is the name of my custom cell.

    //Inside the ViewController
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if let cell:TableViewCell = TableViewCell() {

        cell.stopWatch = stopWatchBlocks[indexPath.row]

        cell.expandButton.tag = indexPath.row

        //Adding action to the expand button
        cell.expandButton.addTarget(self, action: "expandButtonAction1:", forControlEvents: UIControlEvents.TouchUpInside)

        return cell

    }

}

Now, the button action method:

    func expandButtonAction1(button:UIButton) {

    button.selected = !button.selected

    if button.selected {

        indexOfExpendedCell = button.tag
        shouldCellBeExpanded = true

        self.TableView.beginUpdates()
        self.TableView.reloadRowsAtIndexPaths([NSIndexPath(forItem: indexOfExpendedCell, inSection: 0)], withRowAnimation: .Automatic)
        self.TableView.endUpdates()

        button.setTitle("x", forState: UIControlState.Selected)
    }

    else if !button.selected {

        indexOfExpendedCell = button.tag
        shouldCellBeExpanded = false

        self.TableView.beginUpdates()
        self.TableView.reloadRowsAtIndexPaths([NSIndexPath(forItem: indexOfExpendedCell, inSection: 0)], withRowAnimation: .Automatic)
        self.TableView.endUpdates()

        button.setTitle("+", forState: UIControlState.Normal)
    }
}

And Finally the HeightForRowAtIndexPath

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if shouldCellBeExpanded && indexPath.row == indexOfExpendedCell {
        return 166.0
    }
    else { return 91.0 }
}

I think I am missing something because the cell does expand when clicked once, but it does not 'shrink' back to 91 ever!

What am I doing wrong here?

解决方案

Try not to use the selected tag as a toggle for a cell state such as this. Once a cell is selected, tapping it again will not deselect. Instead, you can just use the shouldCellBeExpanded flag:

func expandButtonAction1(button:UIButton) {

    shouldCellBeExpanded = !shouldCellBeExpanded
    indexOfExpendedCell = button.tag

    if shouldCellBeExpanded {
        self.TableView.beginUpdates()
        self.TableView.endUpdates()

        button.setTitle("x", forState: UIControlState.Normal)
    }

    else {
        self.TableView.beginUpdates()
        self.TableView.endUpdates()

        button.setTitle("+", forState: UIControlState.Normal)
    }
}

Also, from my experience the reloadRowsAtIndexPath method is unnecessary. Using the beginUpdates() and endUpdates() alone should do the trick unless you need custom animation.

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

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