I want to add animation when UITableView load cells (From top to Bottom) But getting no idea about this, please someone help me.
Below added a simple table view delegate methode with an array(arrHeader).
Thanks in advance
let arrHeader = ["City1", "City2", "City3"]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrHeader.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell")
cell?.textLabel?.text = arrSubHeader[indexPath.row]
return cell!
}
Put your animation code in this UItableView delegate method:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
//TODO: animation code
}
this delegate method is called every time a new or old cell is going to be displayed on the iPhone/iPad screen.
So if you try something in this block with the cells it will reflect on them.
optional func tableView(_ tableView: UITableView,
didEndDisplaying cell: UITableViewCell,
forRowAt indexPath: IndexPath) { }
this method gets called after a cell is being displayed.
Here is the github link for the Example:
Visit: https://github.com/subhajitregor/TableCellCardDropAnimationExample
Related
I have a tableview implemented. The list shows fine, but I want to detect when the row selection has been changed. I am trying to do a certain action when the row selection is changed, for example, print("row selection has changed ")
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "exploreCell", for: indexPath)
cell.textLabel?.text = tableArray[indexPath.row]
return cell
}
I hope the question is clear.
You can observer selection state from this 2 delegate methods. So when you select any cell it will call didSelectRowAt delegate method and if you tap on selected cell again it will call didDeselectRowAt
Make sure to set tableView selection property to multiple selection incase if you want user to select multiple cell
tableView.allowsMultipleSelection = true
Swift Delegate methods.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Your code here
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
//Your code here
}
Today I have gone through with the very varied behaviour of the table view. It's bounce every time I call reload function as my table view cells are dynamic and having a different type of contents, so I used the property of UITableViewAutomaticDimension for row height.
So, to resolve this bounce issue on the reload function of the table. I have to store the height of the row from tableView willDisplay cell method and used the same in estimatedHeightForRowAt. Please find the code below. Hope it will help someone.
var height_OfCells = NSMutableDictionary()
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = height_OfCells.object(forKey: indexPath) {
return height as! CGFloat
}
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
height_OfCells.setObject(cell.frame.size.height, forKey: indexPath as NSCopying)
}
I'm trying to implement infinite scrolling in UITableView by checking the indexpath at "willDisplay cell", however once the table is loaded with data, the "willDisplay cell" func seems to be drawing all cells before I scroll down although the mobile screen can only fit one or two cells at a time.
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print(indexPath.row)
}
Output without scrolling down:
0
1
2
3
4
5
When I scroll down the "willDisplay cell" func works properly and it prints the indexpath.row one at a time.
What could be the problem here?
Here's a look at the full code:
override func viewDidLoad() {
super.viewDidLoad()
postsTableview.delegate = self
postsTableview.dataSource = self
let nibName = UINib(nibName: "postTableViewCell", bundle:nil)
postsTableview.register(nibName, forCellReuseIdentifier: "postTableViewCell")
loadPosts()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postTableViewCell", for: indexPath) as! postTableViewCell
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print(indexPath.row)
}
I have tried setting the tableview cell height fixed with large value that exceeds the screen height but I still have the same problem
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return 1000.0;
}
It turned out that I needed to set an estimated row height. I'm coming from Android background and this sounds silly to me but it worked out.
postsTableview.estimatedRowHeight = 400
postsTableview.rowHeight = UITableViewAutomaticDimension
On your edited question:
There's nothing wrong with iOS, all table/collection view cells are drawn before they are shown; imagine a table view that shows nothing in the beginning when it's shown, would that be weird?
I don't know what you are trying to achieve after the cells are shown, but I believe that's suitable for another question, good luck!
I have a UITableView nested inside another UITableView.
In the nested tableView, when the user taps on a cell, the cell does highlight, but the delegate method didSelectRowAt indexPath: is not being called.
Other delegate methods are being called, for example methods like willDisplay cell: forRowAt indexPath: or scrollViewDidScroll(_:). So this tells me that the delegates etc. are connected correctly.
In another part of the same application I am using the same kind of structure, UITableView inside another UITableView and there it works fine.
I compared the two extensively, so far I haven't found the difference, and no clue why one should work and the other not!
Please see the implementation of the nested UITableViewCell.
It would be nice if the console would log the line "touch detected".
It does log the lines "scrolling" and "will display cell".
import UIKit
class TestTableViewCell: UITableViewCell {
}
extension TestTableViewCell: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return tableView.dequeueReusableCell(withIdentifier: "FileCell")!
}
}
extension TestTableViewCell: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("touch detected")
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print("will display cell")
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("scrolling")
}
}
Solution was very simple at last.
I had a UITapGestureRecognizer setup for the outer UITableView.
I had this there to dismiss the keyboard on tap.
After removing the UITapGestureRecognizer it started working fine.
I completely overlooked this, but it's solved now!
I am new to swift and havne't programmed in objective C. So i'm new :-)
trying to do something fairly simple. Link a table view cell click to call a method in the controller like so
http://prntscr.com/4m9kf7
Edit:
i have these methods in my MasterController
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
I have added breakpoints in all 4 of them.
and when clicking on the cell it doesn't stop in either of them.
See Screen shot:
http://prntscr.com/4m9ql0
You need to implement the didSelectRowAtIndexPath delegate method and put your handling code inside it.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//CODE TO BE RUN ON CELL TOUCH
}
For Swift 4:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("row selected: \(indexPath.row)")
}