Expand cell at selectedIndex
Updated code below
I am working on comment cells that are limited to 100 characters and if they contain more a show more button will be displayed.
If clicked, the exact cell should reload itself with the number of rows changed to 0 and fully display the cell, no matter how large.
What I have achieved is reloading the cells, but not selected and random.
Below is my code for the extension process
NOTE. Update code for my function
Problem: I have to click the button twice to get the result, minimize and 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 outputs the selected cell (for example, [0, 1] or [0,0], but it does not change then.
While I am hard-coding the code and changing let myIndexPath = IndexPath (line: indexPath! .Row, section: 0)
in let myIndexPath = IndexPath (line: 0, section: 0)
The function works, but randomly reloads some cells and arbitrarily increases and decreases the cell.
In the version variable with the string: indexPath! .Row, the state of the strings is also unchanged, whereas with hardcoded lines it changes from 3 to 0.
Thank you for your help:)
Adding
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!
source to share
Here is the best approach to get the pointer path correct. First, in your method, cellForRow
add the current index line as a tag to your show more button, and then add a click action to your button handler function.
Add to the class the UITableViewCell
output from UIButton
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
}
source to share
You accept the values โโof a class variable and trace tracking. Depending on these variables, you can increase or decrease the cell size and reload it.
In YOURViewController declare variables as:
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() { ...
Connect a button in a cell to this action:
@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")
}
}
}
In the tableview datasource function add a tag to the button:
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
}
And finally, add this delegate method for the cell heights:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if selectedIndex == indexPath.row {
return CGFloat(newSize)
} else {
return CGFloat(defaultSizeOfCell)
}
}
Not to mention, the button must be in a cell and connected to the YOURCustomTableViewCell class like:
class TestTableViewCell: UITableViewCell {
@IBOutlet weak var moreButton: UIButton!
I tested it against your requirements.
source to share