How to rotate image selected row and unrotate image unselected row - uitableview

I have problem liked this:
I have some row, when i click row 0, it expanded and dropdown image rotate. when i click row 1, it expand row 1 and unexpand row 0, dropdown image in row 0 back to normal (down arrow), and dropdown image in row 1 rotate (up arrow).
Here is my code so far:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = self.tableView.cellForRow(at: indexPath) as! PaymentMethodCell
let clickedRow = indexPath.row
if indexPath.row == selectedRow{
print("yang muncul selectedRow = -1")
if cell.isSelected {
selectedRow = -1
print("This cell is deselected :\(selectedRow)")
cell.ivExpand.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat.pi) * 180.0)
}else{
selectedRow = indexPath.row
cell.ivExpand.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat.pi) / 180.0)
}
}else{
if cell.isSelected {
selectedRow = indexPath.row
print("This cell is selected :\(selectedRow)")
cell.ivExpand.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat.pi) / 180.0)
}else{
selectedRow = -1
cell.ivExpand.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat.pi) * 180.0)
}
cell.lblclickedsubcell.text="00"
print("cell.lblclickedsubcell.text:\(String(describing: cell.lblclickedsubcell.text!))")
}
tableView.reloadData()
}
but the result is after i expand row 0 and then click row 1, row 0 unexpand and row 1 expanded. But dropdown image in row 0 still not back to normal (down arrow). it still as up arrow.
Here is cellForRowAt :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "PaymentMethodCell", for: indexPath) as! PaymentMethodCell
cell.ivPaymentMeyhod.contentMode = UIViewContentMode.scaleAspectFit
cell.ivPaymentMeyhod.contentMode = UIViewContentMode.left
cell.ivPaymentMeyhod.clipsToBounds=false
//cell.ivPaymentMeyhod.kf.setImage(with: url!)
cell.ivPaymentMeyhod.image = UIImage(named: "\(PaymentImageArray[indexPath.item])")
let clickedRow = indexPath.row
cell.lblclickedRow.text = "\(indexPath.row)"
let ContractNumber = "\(contractnumber!)"
if(CustomUserDefaults.shared.isGuest() || ContractNumber==""){
cell.lblContractNumber.text = ""
cell.lblContractAmount.text = ""
}else{
cell.lblContractNumber.text = "\(contractnumber!)"
cell.lblContractAmount.text = "\(contractamount!)"
}
if(clickedRow==0){
cell.tableView.isHidden=true
cell.XibView.isHidden=false
let AlfaIndoView = HowToPayView.AlfaIndoNib()
let cellwidth = cell.XibView.bounds.width
print("cellwidth:\(cellwidth)")
cell.XibView.addSubview(AlfaIndoView)
AlfaIndoView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
AlfaIndoView.frame.size.width = cellwidth
}else if(clickedRow==1){
cell.tableView.isHidden=true
cell.XibView.isHidden=false
let POSindonesiaView = HowToPayView.POSindonesiaNib()
let cellwidth = cell.XibView.bounds.width
print("cellwidth:\(cellwidth)")
cell.XibView.addSubview(POSindonesiaView)
POSindonesiaView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
POSindonesiaView.frame.size.width = cellwidth
}else if(clickedRow==2){
cell.tableView.isHidden=false
cell.XibView.isHidden=true
cell.setUpTable()
}else if(clickedRow==3){
cell.tableView.isHidden=false
cell.XibView.isHidden=true
cell.setUpTable()
}else if(clickedRow==4){
cell.tableView.isHidden=false
cell.XibView.isHidden=true
cell.setUpTable()
}else if(clickedRow==5){
cell.tableView.isHidden=false
cell.XibView.isHidden=true
cell.setUpTable()
}else if(clickedRow==6){
cell.tableView.isHidden=true
cell.XibView.isHidden=false
let ATMbersamaView = HowToPayView.ATMbersamaNib()
let cellwidth = cell.XibView.bounds.width
print("cellwidth:\(cellwidth)")
cell.XibView.addSubview(ATMbersamaView)
ATMbersamaView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
ATMbersamaView.frame.size.width = cellwidth
}
return cell
}
Help me, please to repair this code

It would be clearer if we could see your cellForRow code. As far as I can see, you are calling reloadData() at the end, so your cells are getting re-rendered, so you have to handle the arrow's orientations in your cellForRow code. So in pseudocode form, it would be a bit like the following:
In didSelect function:
selectedIndexPath = indexPath
//...other related statements
In cellForRow function:
if indexPath == selectedIndexPath {
//rotate arrow down
//other relevant code
}
else {
//rotate arrow up
//other relevant code
}

Related

TableView didSelect row Issue

So the issue is when a cell is tapped, desired data is shown and when again tapped on same cell ( again desired data is shown.)
But when one cell is selected and we again select other cell (then the data is been shown of second tapped cell but the first one is not deselected).
How can I take care of this issue?
var selectedIndex = -1
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
UIView.animate(withDuration: 1) {
self.labelViewHeightConstraint.constant = 60
self.labelLeadingConstraint.constant = 136
self.view.layoutIfNeeded()
}
let cell = tableView.cellForRow(at: indexPath) as! CustomCell
if(selectedIndex == indexPath.row) {
selectedIndex = -1
print("deselect")
UIView.animate(withDuration: 0.4) {
cell.secondView.isHidden = true
cell.firstView.backgroundColor = UIColor(red: 0.8588, green: 0.84705, blue: 0.8745, alpha: 1.0)
}
} else {
cell.secondView.isHidden = false
}
self.expandTableView.beginUpdates()
//self.expandTableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic )
self.expandTableView.endUpdates()
}
You can archive single selection by setting tableView property like belwo
tableView.allowsMultipleSelection = false
This can also be done from Attributes Inspector
Hope this helps
you must disable multiple selection by,
self.tbl.allowsMultipleSelection = false
and enable single selection by,
self.tbl.allowsSelection = true
EDIT:-
if you want to access your old (selected cells), you should make a call like this,
//first assign tag or indexPath in Cell,
cell.tag = indexPath.row
// or
cell.indexPath = indexPath
//then fetch like bellow,
let visibleCell = tableView.visibleCells.filter({$0.tag == self.selectedIndex})
//or
let visibleCell = tableView.visibleCells.filter({$0.indexPath.row == self.selectedIndex})
//if you use ,
let cell = tableView.cellForRow(at: indexPath) as! CustomCell
//then it will get you new cell object.

UITableView scrolls up on reloadData

I have a UITableView that gets reloaded if a button in some of its cells gets tapped. The issue appears if the following steps are made:
Tap a button on a cell so that another cells appear below the tapped cell on reloadData.
Scroll up the table view so that it hides some of the upper content.
Tap the button again to hide the cells that were just shown making another call to reloadData.
Then the table view goes up and hides the upper content (the whole first cell and part of the second one). Here is some of the code:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if shouldShowImageResolutionOptions && (indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4) {
return isLastKnownDeviceOrientationLandscape ? 60 : 80
}
if shouldShowImageDisplayOptions && (indexPath.row == 3 || indexPath.row == 4) {
return isLastKnownDeviceOrientationLandscape ? 60 : 80
}
return isLastKnownDeviceOrientationLandscape ? tableView.frame.size.height / 2.5 + 40 : tableView.frame.size.height / 4.5 + 40
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if shouldShowImageResolutionOptions {
return 6
}
if shouldShowImageDisplayOptions {
return 5
}
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.IntervalCell) as! IntervalCell
cell.selectionStyle = .none
cell.dottedSliderView.contentMode = .redraw
cell.adjustThumbPosition()
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SettingsCell) as! SettingsCell
cell.selectionStyle = .none
cell.titleLabel.text = LabelTitles.ImageResolution
cell.choiseLabel.text = LabelTitles.HDResolutioin
cell.onButtonTap = {
self.shouldShowImageResolutionOptions = !self.shouldShowImageResolutionOptions
self.shouldShowImageDisplayOptions = false
self.menuTableView.reloadData()
}
return cell
case 2:
if(shouldShowImageResolutionOptions) {
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SingleSettingCell, for: indexPath) as! SingleSettingCell
cell.selectionStyle = .none
cell.mainSettingLabel.text = Settings.HDResolution
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SettingsCell) as! SettingsCell
cell.selectionStyle = .none
cell.titleLabel.text = LabelTitles.ImageDisplay
cell.choiseLabel.text = LabelTitles.EnlargeImage
cell.onButtonTap = {
self.shouldShowImageDisplayOptions = !self.shouldShowImageDisplayOptions
self.shouldShowImageResolutionOptions = false
self.menuTableView.reloadData()
}
return cell
case 3, 4:
if(shouldShowImageResolutionOptions) {
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SingleSettingCell, for: indexPath) as! SingleSettingCell
cell.selectionStyle = .none
cell.mainSettingLabel.text = indexPath.row == 3 ? Settings.HighResolution : Settings.MediumResolution
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SingleSettingCell, for: indexPath) as! SingleSettingCell
cell.selectionStyle = .none
cell.mainSettingLabel.text = indexPath.row == 3 ? Settings.ShowFullImage : Settings.EnlargeImage
return cell
}
case 5:
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifiers.SettingsCell) as! SettingsCell
cell.selectionStyle = .none
cell.titleLabel.text = LabelTitles.ImageDisplay
cell.choiseLabel.text = LabelTitles.EnlargeImage
cell.onButtonTap = {
self.shouldShowImageDisplayOptions = !self.shouldShowImageDisplayOptions
self.shouldShowImageResolutionOptions = false
self.menuTableView.reloadData()
}
return cell
default:
return UITableViewCell()
}
}
From the UITableView's reloadData method documentation (https://developer.apple.com/documentation/uikit/uitableview/1614862-reloaddata):
The table view’s delegate or data source calls this method when it wants the table view to completely reload its data. It should not be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates.
There are dedicated insert/delete rows methods for inserting and deleting:
insertRowsAtIndexPaths:withRowAnimation:(https://developer.apple.com/documentation/uikit/uitableview/1614879-insertrowsatindexpaths)
deleteRowsAtIndexPaths:withRowAnimation: (https://developer.apple.com/documentation/uikit/uitableview/1614960-deleterowsatindexpaths)
So when you refactor your code to use those it should work smoothly and as expected.

Custom collection view hover effect

Can we set collection hover effect on particular cell?
As per below image I set 5th cell as hover but as shown in image how to set 6th cell behind the 5th cell?
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.size = preferredCellSize
var centerX : CGFloat? = 0
if indexPath.item >= 5 {
centerX = preferredCellSize.width / 2.0 + CGFloat(indexPath.item) * (centerDiff) + 20.0
} else {
centerX = preferredCellSize.width / 2.0 + CGFloat(indexPath.item) * centerDiff
}
let centerY = collectionView!.bounds.height / 2.0
attributes.center = CGPoint(x:centerX!, y:centerY)
attributes.zIndex = indexPath.item
return attributes
}

UITableView only shows 1 cell when it should show 2 cells (Swift 3)

So I have 3 cells: SenderCell, ReceiverCell , and a default blank Cell
I want the mainTableView to show all messages from both sender and receiver, which are contained in messageFromCurrentUserArray and messageFromReceiverArray respectively. Unfortunately, whenever I run the code below, it only shows either cell. I would like to show both cells if their arrays aren't empty. Hoping someone could help me. Thanks in advance!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allMessages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < messageFromCurrentUserArray.count {
let cell = mainTableView.dequeueReusableCell(withIdentifier: "SenderCell", for: indexPath) as! SenderTableViewCell
cell.senderMessage.text = messageFromCurrentUserArray[indexPath.row]
mainTableView.beginUpdates()
cell.senderMessage.sizeToFit()
let imageFile = PFUser.current()?["profilePhoto"] as! PFFile
imageFile.getDataInBackground(block: { (data, error) in
if let imageData = data {
self.currentUserImage = UIImage(data: imageData)!
}
})
cell.senderProfilePhoto.image = currentUserImage
cell.senderProfilePhoto.layer.masksToBounds = false
cell.senderProfilePhoto.layer.cornerRadius = cell.senderProfilePhoto.frame.height / 2
cell.senderProfilePhoto.clipsToBounds = true
mainTableView.endUpdates()
return cell
} else if indexPath.row < messageFromReceiverArray.count {
let cell = mainTableView.dequeueReusableCell(withIdentifier: "ReceiverCell", for: indexPath) as! ReceiverTableViewCell
cell.receiverMessage.text = messageFromReceiverArray[indexPath.row]
mainTableView.beginUpdates()
cell.receiverMessage.sizeToFit()
cell.receiverProfilePhoto.image = receiverImage
cell.receiverProfilePhoto.layer.masksToBounds = false
cell.receiverProfilePhoto.layer.cornerRadius = cell.receiverProfilePhoto.frame.height / 2
cell.receiverProfilePhoto.clipsToBounds = true
mainTableView.endUpdates()
return cell
} else {
let cell = mainTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CellTableViewCell
return cell
}
}
The problem is the comparsion in the outer if statements:
if indexPath.row < messageFromCurrentUserArray.count {
//...
} else if indexPath.row < messageFromReceiverArray.count {
// ...
} else {
// ...
}
Since all the counts in the arrays start with index 0 and stop at their count each, but your indexPath.row will go up to the the sum of both (e.g. messageFromCurrentUserArray.count + messageFromReceiverArray.count)
You'll have to change the second if clause and the index access to something like
} else if indexPath.row < (messageFromCurrentUserArray.count + messageFromReceiverArray.count) {
// ...
int receiverIndex = indexPath.row - messageFromCurrentUserArray.count
cell.receiverMessage.text = messageFromReceiverArray[receiverIndex]
// or: cell.receiverMessage.text = allMessages[indexPath.row]
// ...
} else {
// ...
}

How to add animation to UIViews inside a UITableViewCell?

I'm currently developing an iOS app using swift and I get some problem trying to add animations to UIViews inside a cell.
It's a menu using UITableView with sections.
Some of the cells can be expanded, by press a button in the cell.
The button is an arrow like ">" but pointing to the bottom , when pressed it should become up side down and the cell expand. Press it again the arrow point to bottom again and the cell shrink.I want to add an animation to it .
Animation
extension UIView{
func addAnimation_rotation_x (from : Double , to : Double){
let rotationAnimation = CABasicAnimation()
rotationAnimation.keyPath = "transform.rotation.x"
rotationAnimation.duration = 0.3
rotationAnimation.removedOnCompletion = false
rotationAnimation.fillMode = kCAFillModeForwards
rotationAnimation.fromValue = from
rotationAnimation.toValue = to
self.layer.addAnimation(rotationAnimation, forKey: nil)
}
}
ViewController
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if index_of_expandedRow.contains(indexPath){
let height = CGFloat(self.total_dishArray[indexPath.section][indexPath.row].expanded_rows.count) * 60 + 150
return height
}else{
return 150
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCellWithIdentifier("DishTableViewCell") as! DishTableViewCell
if isFinishedLoadingDishes{
//setting cells
cell.btn_expand.addTarget(self, action: #selector(OrderViewController.expand(_:)), forControlEvents: UIControlEvents.TouchUpInside)
if index_of_expandedRow.contains(indexPath){
cell.btn_expand.addAnimation_rotation_y(0, to: M_PI)
}else{
cell.btn_expand.addAnimation_rotation_y(M_PI, to: 0)
}
}
return cell
}
func expand (sender :UIButton){
let cell = sender.superview!.superview as! UITableViewCell
let indexpath = tableView_right.indexPathForCell(cell)!
if self.index_of_expandedRow.contains(indexpath){
let index = self.index_of_expandedRow.indexOf(indexpath)!
self.index_of_expandedRow.removeAtIndex(index)
}else{
self.index_of_expandedRow.append(indexpath)
}
tableView_right.reloadRowsAtIndexPaths([indexpath], withRowAnimation: UITableViewRowAnimation.None)
//if the expanded content is not visible then make it visible
let frame = self.tableView_right.cellForRowAtIndexPath(indexpath)!.frame
if frame.origin.y + frame.height > tableView_right.frame.height + tableView_right.contentOffset.y {
tableView_right.setContentOffset(CGPointMake(0, tableView_right.contentOffset.y + ((frame.origin.y + frame.height) - (tableView_right.frame.height + tableView_right.contentOffset.y))) ,animated: true)
}
}
I expand the rows by changing their height.
The animation worked well when hitting the button the first time , but failed after.
Any suggestion would be appreciated.

Resources