UITableViewController and prefetching - swift

In my older Swift 3 app I have been using a UITableView and I implemented the function
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath])
Now I redesign the app and would like to use a UITableViewController instead and cant find a similar function to prefetch. Is there a similar function for the TableViewController?
I am using the table to show async downloaded images and pdf:s.

Simply have your controller adopt the UITableViewDataSourcePrefetching protocol. The function will then be available (and required).
As you point out in your comment, you also have to set the prefetchDataSource of the table view.

Related

The tableView(_ tableView: UITableView, editingStyleForRowAt in a class results in inconsistent behaviour in IOS 9.3 & IOS 10+

Using the following code in a class that has more than one edit action - slide to left and move row results in IOS 9.3 devices not allowing the slide to left but allows the move. Both actions work fine in IOS 10+.
Removing this code allows 9.3 & 10+ to do both actions however the red delete button now appears on the left when a move row action is instituted
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return .none
}
The code is in an UIViewController class which has a table that has a single prototype cell.
Every other feature works without any issues.
Is there any way to selectively call this function. ie call it only for IOS 10+ devices?
If you wand to use something for certain iOS:
if #available(iOS 10.0, *) {
//do what you need
}
But you can specify this already in the function itself.

Adopting Drag and Drop in a Table View / Collection View not working on iPhone

Does anyone know why the Apple example of Adopting Drag and Drop in a Table View is not working on iPhone?
Steps to Reproduce:
Download code from https://developer.apple.com/documentation/uikit/drag_and_drop/adopting_drag_and_drop_in_a_table_view
Open project and change Deployment target -> Devices to Universal.
Run application on iPhone (simulator).
Try to drag some cell.
Drag and drop functionality is not working but it should behave the same way as on the iPad devices. Even the function
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem]
is not called.
Configuration:
Xcode Version 9.0 (9A235)
Apple Swift version 4.0 (swiftlang-900.0.63 clang-900.0.37)
Simulator Version 10.0 (SimulatorApp-829.6
CoreSimulator-494.13.6)
UITableView & UICollectionView have dragInteractionEnabled: Bool instance properties.
Xcode's documentation:
The default value of this property is true on iPad and false on iPhone. Changing the value to true on iPhone makes it possible to drag content from the table view to another app on iPhone and to receive content from other apps.
In TableViewController.swift, add
tableView.dragInteractionEnabled = true
into viewDidLoad() (I've added it just before the dragDelegate and dropDelegate are set, and it seems to work fine).

When does heightForHeaderInSection gets called

I am calling the following function to update the tableView. However, I am finding that heightForHeaderInSection gets called twice for each section there are in the table. So if I have 5 sections, the function gets called 10 times. Is that normal?
tableView.beginUpdates()
let sections = NSIndexSet(index:posts.count - 1)
tableView.insertSections(sections, withRowAnimation: .None)
tableView.endUpdates()
To answer youre question lets look at UITableViewDelegate. From Apple documentation :
The delegate of a UITableView object must adopt the
UITableViewDelegate protocol. Optional methods of the protocol allow
the delegate to manage selections, configure section headings and
footers, help to delete and reorder cells, and perform other actions.
So when UITableViewDelegate is get called in your'e case? Delegate is get called when you update tableView with insertSections. So heightForHeaderInSection is called after you insert new sections to tableView.
Amount of times that heightForHeaderInSection is get called depends on how you update tableView.
Also it is possible to highlight that Apple do not gives clear explanation how often UIKit would call you're delegate methods.

Ambiguous reference to member 'tableView'

Code and errors
Continued on the code and one more errors!
Hi, I'm new to Xcode swift. I have been doing good so far except trying to follow someone tutorial from 8.2 and I'm getting these errors that I have the hardest time understanding. Anybody who can explain what these errors means and maybe, just maybe how to fix them?
I have edited my posts, can't link more items than 2.
You need to declare your tableView up by your other #IBOutlets since you are using a UIViewController and putting a tableView within it's view. Currently the UIViewController doesn't know what tableView you are referring too.
#IBOutlet var tableView: UITableView!
Then link it up in the interface builder as you have done with your other #IBOutlets. Make sure you link the delegate and dataSource properties of your tableView back to the view controller as well.
To do the latter, after you select your tableView, select the Connections Inspector area, as shown in the picture below, and connect them back to your UIViewController.
In my case, I've used:
#IBOutlet private weak var tableView: UITableView!
and use tableView inside extensions method.
I've change private to fileprivate, and error has gone.
Here it is in Swift 4
let iPath = NSIndexPath(row: self.TableView.numberOfRows(inSection: 0)-1,
section: self.messageTableView.numberOfSections-1)
self.TableView.scrollToRow(at: iPath as IndexPath,
at: UITableViewScrollPosition.bottom,
animated: true)

CellForRowAtIndexPath method not being called in storyboards

I am using storyboards for my app.In that I have a UITableViewController class.I am loading the UITableView from the data coming from the webservice. The issue is that the data is coming but is not geting populated in UITableView. On Decoding I found out that the cellForRowAtIndexPath method is not getting called.
Do we need to connect the datasource and delegate in storyboard as it was done in separate xibs before storyboard. And if so, where to connect the datasource and delegate methods as there is NO Filesowner in storyboard.
I am stuck up with this issue and any help would be appreciated.
Thanks
If you put a table view controller into a storyboard, it usually has the table view's dataSource and delegate already set up correctly. If yours turn out to be connected OK, the other possibility is that tableView:numberOfRowsInSection: is returning zero.
If you are using the UITableViewController, then you need to make the numberOfSections:tableView: data source method returns 1 instead of the default return of 0.
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1 // Default is 0, should be greater than 0
}