Reputation: 1239
I was looking around in this forum but haven't found anything related this.
I would like to take control of UITableView's selection function: style the cell on select & deselect, and make it remain after being reused.
The action when the user click is coming with animation, it is decleared in the TableViewController
's file of course:
func selectWorldMessage(indexPath: IndexPath) {
...
cell.attributedText = worldMessage.message.wholeWorldMessageAttributedString()
UIView.animate(withDuration: duration, animations: {
cell.bubbleImageView.tintColor = appColors.worldMessageBubbleSelected
cell.timeLabel.isHidden = false
cell.messageLabelBottomConstraint.constant = 14
cell.messageLabelTopConstraint.constant = 14
cell.timeLabel.alpha = 1.0
self.view.layoutIfNeeded()
self.tableView.beginUpdates()
self.tableView.endUpdates()
self.lastContentOffsetY = nil
}, completion: nil)
...
func deselectWorldMessage(indexPath: IndexPath) {
...
But.. let's assume you scroll down and up, the cell gets reused. I have to style it again. Where should I do it?
A) In the TableViewController
's cellForRowAt
function?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
....
// If previously was selected
if (currentSelectedIndexPath == indexPath) {
cell.messageLabel.attributedText = worldMessage.message.wholeWorldMessageAttributedString()
cell.bubbleImageView.tintColor = appColors.worldMessageBubbleSelected
cell.timeLabel.isHidden = false
cell.messageLabelBottomConstraint.constant = 14
cell.messageLabelTopConstraint.constant = 14
} else {
cell.messageLabel.attributedText = worldMessage.message.shortenWorldMessageIfNeededAttributedString()
cell.bubbleImageView.tintColor = appColors.worldMessageBubble
cell.timeLabel.isHidden = true
cell.messageLabelBottomConstraint.constant = 10
cell.messageLabelTopConstraint.constant = 10
}
B) Or in the Cell
's setSelected
function?
override func setSelected(_ selected: Bool, animated: Bool) {
if selected == true {
self.messageLabel.attributedText = worldMessage.message.wholeWorldMessageAttributedString()
self.bubbleImageView.tintColor = appColors.worldMessageBubbleSelected
self.timeLabel.isHidden = false
self.messageLabelBottomConstraint.constant = 14
self.messageLabelTopConstraint.constant = 14
} else {
self.messageLabel.attributedText = worldMessage.message.shortenWorldMessageIfNeededAttributedString()
self.bubbleImageView.tintColor = appColors.worldMessageBubble
self.timeLabel.isHidden = true
self.messageLabelBottomConstraint.constant = 10
self.messageLabelTopConstraint.constant = 10
}
}
What would be better & less energy consuming?
Upvotes: 0
Views: 73
Reputation: 671
The code should be inside your cell file because It is cell related things and It will remain after being reused. It's to your view controller to handle the "filling" logic for your cells but in this case you want to change UI style of the cells. If you put the code in view controller, when you change the cell (by adding or removing a label), you have to change your controller too. Also you will need to save selected index path to check in cellForRowAt method.
class TableViewCell: UITableViewCell {
override func setSelected(_ selected: Bool, animated: Bool) {
if selected {
self.backgroundColor = .red
} else {
self.backgroundColor = .green
}
}
}
Upvotes: 1