Expand Cell at selectedIndex

Viewed 318

Updated Code Below

I am working on comment cells who are limited to 100 characters and if they contain more a "show more button" will show up.

If pressed, the exact cell should reload itself with the number of lines changed to 0 and fully display the cell, no matter how big.

What I have achieved is that cells reload, but not the selected one and kinda arbitrary.

Below is my code for the enlarging process

NOTE: Updatet Code for My Function

Problem: I have to press the button twice to get the result, to minimize and to maximize the cell

   @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))!)
    })
}

The index comes from

extension CommentTableViewCell {

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

Note

The print statement prints out the chosen cell ( e.g. [0, 1] or [0,0] but it doesn't change then.

Whereas I hardcode my code and change let myIndexPath = IndexPath(row: indexPath!.row, section: 0)

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

The feature works, but arbitrarily reloads some cells and arbitrarily enlarges and decreases the cell.

In the variable version with row: indexPath!.row the lines state doesn't change as well, whereas with hardcoded the lines change between 3 and 0.

Thanks for your help :)

Addition

my commentCell

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 Answers
Related