Search code examples
swiftfirebasetableviewrowcell

Expand Cell at selectedIndex


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!

Solution

  • Here is a better approach to get you the correct index path. First, in your cellForRow method, add the current index row as tag to your show more button, and then add click action to your button handler function.

    Add an outlet of UIButton in you custom UITableViewCell class as

    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
    }
    

    Then in your handler function, you can get the correct index path by reading this tag

    func tapForMore(sender: UIButton) {
        let myIndexPath = IndexPath(row: sender.tag, section: 0)
        print("myindex", myIndexPath)
        //... other code here
    }