在selectedIndex上展开Cell

时间:2017-06-30 11:19:43

标签: swift firebase tableview row cell

更新了以下代码

我正在处理限制为100个字符的评论单元格,如果它们包含更多,则会显示“显示更多按钮”。

如果按下,确切的单元格应重新加载,行数变为0并完全显示单元格,无论多大。

我所取得的成就是细胞重新加载,而不是选择的细胞,并且有点任意。

以下是放大过程的代码

注意:更新我的功能代码

问题:我必须按两次按钮才能得到结果,以最小化并最大化细胞

   @IBAction func readMore(_ sender: UIButton) {


    self.state = !self.state

    print("state" , state)
    self.tapMore.setTitle(self.state ? self.decreaseState: self.expandState, for: .normal)
    self.commentLabel.numberOfLines = (self.state ? self.expandedLines: self.numberOfLines)
    print(self.commentLabel.numberOfLines)
    let myIndexPath = IndexPath(row: sender.tag, section: 0)

    UIView.animate(withDuration: 0.3, animations: {
        self.parentViewControllerCommentCell?.tableView.reloadRows(at: [myIndexPath], with: UITableViewRowAnimation(rawValue: Int(UITableViewAutomaticDimension))!)
    })
}

索引来自

extension CommentTableViewCell {

var indexPath: IndexPath? {
    return (superview as? UITableView)?.indexPath(for: self)
   }
}

注意

print语句打印出所选的单元格(例如[0,1]或[0,0],但它不会改变。

然后我对代码进行硬编码并进行更改     让myIndexPath = IndexPath(row:indexPath!.row,section:0)

要     let myIndexPath = IndexPath(row:0,section:0)

该功能有效,但可以随意重新加载一些单元格,并随意放大和缩小单元格。

在带有row:indexPath!.row的变量版本中,行状态也不会改变,而使用硬编码时,行在3和0之间变化。

感谢您的帮助:)

加成

我的评论细胞

class CommentTableViewCell: UITableViewCell {

@IBOutlet weak var likeCountButton: UIButton!
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var commentLabel: KILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var likeImageView: UIImageView!


@IBOutlet weak var tapMore: UIButton!

@IBOutlet weak var tapMoreButton: UIButton!


var delegate: CommentTableViewCellDelegate?
var postId : String!

2 个答案:

答案 0 :(得分:1)

这是一种更好的方法来获取正确的索引路径。首先,在cellForRow方法中,将当前索引行作为标记添加到show more按钮,然后将click操作添加到按钮处理函数中。

在自定义UIButton课程中添加UITableViewCell的插座

class CustomCell: UITableViewCell {
     @IBOutlet var moreButton: UIButton! // Connect your button from storyboard
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
    cell.moreButton.tag = indexPath.row
    /* Just add action normally from storyboard. No need to add target. cell.moreButton.addTarget(self, action:#selector(buttonUp(sender:)), for: .touchUpInside) */

    return cell
}

然后在处理程序函数中,您可以通过读取此标记

来获取正确的索引路径
func tapForMore(sender: UIButton) {
    let myIndexPath = IndexPath(row: sender.tag, section: 0)
    print("myindex", myIndexPath)
    //... other code here
}

答案 1 :(得分:0)

您可以使用类变量并跟踪点击次数。根据这些变量,您可以增加或减少单元格的大小并重新加载它。

在YOURViewController中将变量声明为:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

       @IBOutlet weak var CommentsTableView: UITableView!
       var defaultSizeOfCell = 60.0
       var newSize = 80.0
       var selectedIndex = -1

       var isExpanded = false
       var expandCounter = 0
       override func viewDidLoad() { ...

将单元格中的按钮连接到此操作:

@IBAction func moreButtonAction(_ sender: UIButton) {

    if !isExpanded {
        if expandCounter == 0 {
            expandCounter = expandCounter + 1
        } else if expandCounter == 1 {
            expandCounter = 0
            isExpanded = true
            selectedIndex = sender.tag
            let myIndexPath = IndexPath(row: sender.tag, section: 0)

            UIView.animate(withDuration: 0.3, animations: {
                self.CommentsTableView.reloadRows(at: [myIndexPath], with: UITableViewRowAnimation(rawValue: Int(UITableViewAutomaticDimension))!)
            })
            print("Increase")
        }
    } else if isExpanded {
        if expandCounter == 0 {
            expandCounter = expandCounter + 1
        } else if expandCounter == 1 {
            expandCounter = 0
            isExpanded = false
            selectedIndex = -1
            let myIndexPath = IndexPath(row: sender.tag, section: 0)

            UIView.animate(withDuration: 0.3, animations: {
                self.CommentsTableView.reloadRows(at: [myIndexPath], with: UITableViewRowAnimation(rawValue: Int(UITableViewAutomaticDimension))!)
            })
            print("Decrease")
        }
    }


}

在tableview数据源函数中,将标签添加到按钮:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath) as! TestTableViewCell
    cell.moreButton.tag = indexPath.row
    return cell
}

最后为细胞高度添加此委托方法:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if selectedIndex == indexPath.row {
        return CGFloat(newSize)
    } else {

        return CGFloat(defaultSizeOfCell)

    }

}

更不用说,按钮应该在单元格中并连接到YOURCustomTableViewCell类:

class TestTableViewCell: UITableViewCell {

      @IBOutlet weak var moreButton: UIButton!

我已根据您的要求对其进行了测试。