I am reloading table view every 0.25 seconds as I have a timer app, but the delete button when I swipe from right to left won't stay there because I am reloading the table view.
How do I fix this?
There is no need to reload the tableView to update the visible cells. Instead, loop over the visible cells calling the update method on each one:
// Example custom cell class
class MyCustomCell: UITableViewCell {
func updateTimeLabel() {
// code to update time label
}
}
In the timer method:
for case let cell as MyCustomCell in tableView.visibleCells {
cell.updateTimeLabel()
}
Your updateTimeLabel method might need data such as the current Date or the new value for the time label, so pass it what it needs to update the label.
a start of solution could be to observe swipe events on tableview thanks to :
optional func tableView(_ tableView: UITableView, willBeginEditingRowAtIndexPath indexPath: NSIndexPath)
in this func you stop the reload func
and in :
optional func tableView(_ tableView: UITableView, didEndEditingRowAtIndexPath indexPath: NSIndexPath)
you restart your reload func
Related
I have the parent custom Collectionviewcell and inside I have the child custom Tableview. When I click table view I need to change the background color of collectionviewcell and tableview row background. I am trying achieve using gesture for tableview. but its not working.
try with protocols
first your tableview inform the collection cell
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
backgroundColor = .red
delegate?.didSelectItem(indexPath: indexPath)
}
then on your collectionView Cell
func didSelectItem(indexPath: IndexPath) {
self.backgroundColor = .blue
}
Why you don't use didSelect of tableView?
Some code should be easy to see how you use gestures.
I have created one main array, inside created another array for table content, and manage selection on tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath).
Please check demo : https://github.com/karan7698007/CollectionViewDemo.git
I am having trouble selecting table view row in didSelectRowAt/didSelectRowAtIndexPath. The function is never called.
func tableView(tableView: UITableView, didSelectRowAt indexPath: NSIndexPath) {
print("Row: ", indexPath.row)
if(indexPath.row == 9)
{
print("Row 9 Selected")
}
}
Based on various Stack Overflow articles, here is what I have tried:
I have tried "didSelectRowAt" and "didSelectRowAtIndexPath"
I have set the table view's delegate to self using its outlet
Table View Settings:
-Selection: Single Selection
-Editing: No Selection During Editing
-Interaction: User Interaction Enabled
Individual Cell Settings:
-Interaction: User Interaction Enabled
I set tableview.allowsInteraction to true in code as well as storyboard
Should be with _
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Check Docs
The problem was that I was using NSIndexPath instead of IndexPath
check delegate value using break point either it is nil or not
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Code
}
You have to set an #IBOutlet to the tableView in you ViewController and set as it's delegate and dataSource.
Something like this :
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
And implements the UITableViewDataSource protocol too.
Or you can too in the Interface Builder set the ViewController as it's delegate and dataSource and avoid to set manually in code like above.
If you have more than one tableView I your code than please try to mention different names for every tableView and then call the delegate with particular tableView name.
Also you have mentioned 3rd point here “Single Selection -Editing: No Selection During Editing”. This means if you try to edit anything in tableView there will be no selection please try to replace this and then check if you get some result.
Because if set “tableView.allowsSelectionDuringEditing = false” then it will not allow you selections in tableView.
I have a tableview and I have 20 prototype cells and tableview's Content parameter is Dynamic Prototypes
At a time I show only one protoype cell but for some weird reason cellForRowAtIndexPath is getting called twice.
I have gone through my code and I call reloaddata just once.
I couldn't figure out what could have caused tableview to call cellForRowAtIndexPath twice !
so i generally would like to know what and all could cause tableview to call cellForRowAtIndexPath more than once for the same row
Update:
In one of the prototype cells if a button is clicked then probably I would reload the data so that I can show some other prototype cell but then also its called twice
but still number of rows count is one
If you call reloadData() explicitly yourself, that would explain it. The tableView loads data by default in UITableViewController, so explicitly calling it again would trigger cellForRowAt the second time. You can easily confirm this by removing an explicit call to reloadData() - then the tableView should look correctly and the cellForRowAt should be called only once.
I tested a following minimal working example in Playgrounds, and it got called only once:
import UIKit
import PlaygroundSupport
class A: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Reload", style: .plain, target: self, action: #selector(buttonPressed))
}
#objc func buttonPressed() {
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(">>>> calling numberOfRowsInSection")
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print(">>>> \(indexPath)")
return UITableViewCell()
}
}
PlaygroundPage.current.liveView = UINavigationController(rootViewController: A())
So unless you do something else, the cellForRowAt should be called really just once per reloadData().
By default there is a call when view controller is initialized to reload the tableView when you add another one then it's 2
I have a UIView with 3 nested UIViews. Their total width is 300% of screen. (100% each).
I'm changing AutoLayout priorities to bring the required on the screen and others off screen with buttons click from NavBar.
|uiview1| |uiview2| |uiview3|
uiview3 has a UITableView. It populates, scrolls perfectly, but on tapping an item, the item gets highlighted for a second and then released. The function 'didSelectRowAt' is NOT being called.
I have tried disabling every other gesture registered on this VC. The click is detected too but doesn't persist and gets deselected instantly.
//globally in VC
#IBOutlet weak var salamsTable: UITableView!
//in viewDidLoad
self.salamsTable.delegate = self
self.salamsTable.dataSource = self
//delegate functions
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
print (indexPath.row) // this never gets called
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.allSalam.count //works perfectly
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell = self.msgsTable.dequeueReusableCell(withIdentifier: "messages") as! MessageCell
return cell // works perfectly
}
Visual Demo:
Not a Solution but an Alternate. Things worked out in another way. As you can see in the Demo GIF attached in the question, the cells are being highlighted properly. So i add the delegate method didHighlightRowAt and it worked like a charm.
I have a ViewController that contains a collectionview and a tableview. I would like to be able to press a row in my tableView and update the collectionView with new images. I'm not sure how I can reference the collectionView in this scenario.
I'm assuming I would have to put some code in my "DidSelectRowAt" method in my tableView, and pass data using dictionaries. Anyone have any idea? thanks!
Try this method and pass your latest dictionary to collectionView Dictionary
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
yourCollectionViewDict = arrDict[indexPath.row]
collectionView.reloadData()
}
Have you tried triggering collectionView.reloadData() from your tableView's didSelectRowAtIndexPath?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
collectionView.reloadData()
}