How to optimize loading from Firestore to Tableview - ios

My View Controller has a Tableview with 2 segments. Depending on which Segment is selected, the Tableview displays a different set of data.
#IBAction func didChangeSegment(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
model.getRecipes(starredTrue: false)
}
else if sender.selectedSegmentIndex == 1 {
model.getRecipes(userAdded: true)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recipe.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MealPlanCell", for: indexPath) as! MealPlanCell
let recipeInTable = recipe[indexPath.row]
cell.displayRecipe(recipe: recipeInTable, indexPathRow: indexPath.row)
return cell
}
And this is how model.getRecipes() gets data from Firestore before returning it to the Tableview:
let recipeQuery = db.collection("recipes")
let docRef = recipeQuery.document(documentId)
docRef.getDocument { document, error in
if let error = error as NSError? {
self.errorMessage = "Error getting document: \(error.localizedDescription)"
}
else {
if let document = document {
do {
self.recipe = try document.data(as: Recipe.self)
let recipeFromFirestore = Recipe(
id: documentId,
title: self.recipe!.title ?? "")
self.recipes.append(recipeFromFirestore)
}
catch {
}
}
DispatchQueue.main.async {
self.delegateRecipes?.recipesRetrieved(recipes: self.recipes)
}
}
}
The issue I'm having is that the Tableview takes a very long time to display data. It appears this is because it has to wait for the model to finish loading all the data from Firestore every time I select one of the segments.
How can I optimize this process? Is it possible to have the TableView load/display cell by cell, instead of needing to wait for all data to be loaded?
Any guidance is much appreciated!

Related

Reload TableView After Deleting, Adding, or Modifying Firestore Document and Paginating Results

I am retrieving documents from Firebase Firestore and displaying them in a table view. From the table view I want to be able to delete and add items. I also modify documents from the item detail view. I'll focus on my issues deleting items for this question though. I'm getting paginated results with my query by using the last snapshot to only get the next set of items. I'm also using a listener to get realtime updates for when items are modified. The issue with deleting is how to I handle it correctly? What I currently have deletes items just fine but then doubles the remaining rows in the table view.
var items = [Item]()
var itemQuery: Query?
var lastSnapshot: QueryDocumentSnapshot?
func getItems() {
if lastSnapshot == nil {
itemQuery = Firestore.firestore().collection("items").whereField("collection", isEqualTo: self.collection!.id).order(by: "name").limit(to: 25)
} else {
itemQuery = itemQuery?.start(afterDocument: lastSnapshot!)
}
itemQuery!.addSnapshotListener( { (snapshot, error) in
guard let snapshot = snapshot else {
return
}
if snapshot.documents.last != nil {
self.lastSnapshot = snapshot.documents.last
} else {
return
}
if let error = error {
print(error.localizedDescription)
} else {
for document in snapshot.documents {
let docName = document["name"] as? String
let docId = document.documentID
let docImages = document["images"] as? [String]
let docCollection = document["collection"] as? String
let docInfo = document["info"] as? String
let docQuantity = document["quantity"] as? Int
let item = Item(id: docId, name: docName!, collection: docCollection!, info: docInfo!, images: docImages!, quantity: docQuantity!)
self.items.append(item)
}
if self.items.count >= 25 {
self.addFooter()
}
self.tableView.reloadData()
}
})
}
func deleteItem(at indexPath: IndexPath) {
let itemToDelete = items[indexPath.row]
// Delete images from storage
for url in itemToDelete.images {
let store = Storage.storage()
let storageRef = store.reference(forURL: url)
storageRef.delete { error in
if let error = error {
print(error.localizedDescription)
} else {
print("Image file deleted successfully")
}
}
}
Firestore.firestore().collection("items").document(itemToDelete.id).delete() { error in
if let error = error {
print(error.localizedDescription)
} else {
print("Item deleted")
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("numberOfRows(): \(items.count)")
return items.count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
let item = items[indexPath.row]
cell.itemNameLabel.text = item.name
if item.images.count > 0 {
let thumbnailUrl = item.images[0]
cell.itemImageView.sd_setImage(with: URL(string: thumbnailUrl), placeholderImage: UIImage(named: "photo"), completed: { (image, error, cacheType, imageUrl) in
cell.itemImageView.roundCornersForAspectFit(radius: 10)
})
} else {
cell.itemImageView.image = UIImage(named: "photo")
}
return cell
}
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Items before delete: \(items.count)")
deleteItem(at: indexPath)
// items.removeAll()
// tableView.reloadData()
items.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
print("Items after delete: \(items.count)")
}
}
You can use Property Observer to handle your tableView.reloadData()
var items = [Item]() {
didSet {
tableView.reloadData()
}
}
what it does above is whenever variable items is modified, it will trigger didSet {} block of code.
Hope is will answer your question.

tableview doesn't refresh deleted data

My table view does not refresh. it keep the old values of deleted data
and can show new added values.
it only refresh(remove deleted values) when i close the app and open it again.
here is my code
private func observeChannels() {
let userEmail = Auth.auth().currentUser?.email
channelRefHandle = channelRef.observe(.childChanged, with: { (snapshot) -> Void in
let channelData = snapshot.value as! Dictionary<String, AnyObject>
let id = snapshot.key
let groupimage = channelData["groupImage"] as? String!
let descc = channelData["desc"] as? String!
let groupCountvar = channelData["Members"]?.count
let groupTasksVar = channelData["Tasks"]?.count
if let name = channelData["name"] as! String!, name.characters.count > 0 {
//members snapshot
if let childSnapshot = snapshot.childSnapshot(forPath: "Members") as? DataSnapshot{
if let membersDictionary = childSnapshot.value as? [String:AnyObject] , membersDictionary.count > 0{
for membersDictionary in membersDictionary {
if userEmail == membersDictionary.value as? String {
self.groups.append(Group(id: id,name: name, createdBy: (userEmail)!, desc: (descc)!, groupImage: (groupimage)!, groupCount: ("\(groupCountvar!)") , groupTasksCount: ("\(groupCountvar!)")))
print(membersDictionary.value)
}
}
}
self.tableView.reloadData()
}}else {
print("Error!")
self.tableView.reloadData()
}
})}
datasource / delegate
override func numberOfSections(in tableView: UITableView) -> Int {
return 2 }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let currentSection: Section = Section(rawValue: section) {
switch currentSection {
case .createNewChannelSection:
return 0
case .currentChannelsSection:
return groups.count
}
} else {
return 0 }
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 75.0 }
//tableView
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExistingChannel", for: indexPath) as! GroupTableViewCell
if (indexPath as NSIndexPath).section == Section.currentChannelsSection.rawValue {
cell.goupName?.text = groups[(indexPath as NSIndexPath).row].name
cell.groupDesc?.text = groups[(indexPath as NSIndexPath).row].desc
cell.groupimg?.image = UIImage(named: groups[(indexPath as NSIndexPath).row].groupImage)
cell.numbMembLbl?.text = groups[(indexPath as NSIndexPath).row].groupCount
cell.taskNumbLbl?.text = groups[(indexPath as NSIndexPath).row].groupTasksCount
}
return cell }
// MARK: UITableViewDelegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (indexPath as NSIndexPath).section == Section.currentChannelsSection.rawValue {
let channel = groups[(indexPath as NSIndexPath).row]
self.performSegue(withIdentifier: "ShowChannel", sender: channel)
} }
this is my table view i dont know why the deleted/removed doesn't observe.
You are observing the node for .childChanged events only.
That means the code in the closure will only fire if a child is changed.
From the documentation
FIRDataEventTypeChildChanged - Listen for changes to the items in a
list. This event is triggered any time a child node is modified. This
includes any modifications to descendants of the child node. The
snapshot passed to the event listener contains the updated data for
the child.
So that means the code in your closure will only be called when a child is changed, and will not be called when a child is added or deleted.
You need to add additional observers for .childAdded events and .childRemoved events to handle those cases. When you receive a childAdded event, add that snapshot data to your array and likewise when you receive a childRemoved event, remove it from the array.
Remember to reload your table view in all cases so the UI updates with the fresh data.
I personally would change this line to make it more readable
for membersDictionary in membersDictionary {
to
for member in membersDictionary {
but that's just a style thing.
Try using this for reloading tableView
DispatchQueue.main.async {
self.tableView.reloadData()
}
This will help.
you need this :
func tableView(_ tableView: UITableView, commit editingStyle:
UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Deleted")
self.yourArrayName.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
It'll do the job.

Limit the amount of cells shown in tableView, load more cells when scroll to last cell

I'm trying to set up a table view that only shows a specific amount of cells. Once that cell has been shown, the user can keep scrolling to show more cells. As of right now I'm retrieving all the JSON data to be shown in viewDidLoad and storing them in an array. Just for example purposes I'm trying to only show 2 cells at first, one the user scrolls to bottom of screen the next cell will appear. This is my code so far:
class DrinkViewController: UIViewController {
#IBOutlet weak var drinkTableView: UITableView!
private let networkManager = NetworkManager.sharedManager
fileprivate var totalDrinksArray: [CocktailModel] = []
fileprivate var drinkImage: UIImage?
fileprivate let DRINK_CELL_REUSE_IDENTIFIER = "drinkCell"
fileprivate let DRINK_SEGUE = "detailDrinkSegue"
var drinksPerPage = 2
var loadingData = false
override func viewDidLoad() {
super.viewDidLoad()
drinkTableView.delegate = self
drinkTableView.dataSource = self
networkManager.getJSONData(function: urlFunction.search, catagory: urlCatagory.cocktail, listCatagory: nil, drinkType: "margarita", isList: false, completion: { data in
self.parseJSONData(data)
})
}
}
extension DrinkViewController {
//MARK: JSON parser
fileprivate func parseJSONData(_ jsonData: Data?){
if let data = jsonData {
do {
let jsonDictionary = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String : AnyObject]//Parses data into a dictionary
// print(jsonDictionary!)
if let drinkDictionary = jsonDictionary!["drinks"] as? [[String: Any]] {
for drink in drinkDictionary {
let drinkName = drink["strDrink"] as? String ?? ""
let catagory = drink["strCategory"] as? String
let drinkTypeIBA = drink["strIBA"] as? String
let alcoholicType = drink["strAlcoholic"] as? String
let glassType = drink["strGlass"] as? String
let drinkInstructions = drink["strInstructions"] as? String
let drinkThumbnailUrl = drink["strDrinkThumb"] as? String
let cocktailDrink = CocktailModel(drinkName: drinkName, catagory: catagory, drinkTypeIBA: drinkTypeIBA, alcoholicType: alcoholicType, glassType: glassType, drinkInstructions: drinkInstructions, drinkThumbnailUrl: drinkThumbnailUrl)
self.totalDrinksArray.append(cocktailDrink)
}
}
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
}
DispatchQueue.main.async {
self.drinkTableView.reloadData()
}
}
//MARK: Image Downloader
func updateImage (imageUrl: String, onSucceed: #escaping () -> Void, onFailure: #escaping (_ error:NSError)-> Void){
//named imageData because this is the data to be used to get image, can be named anything
networkManager.downloadImage(imageUrl: imageUrl, onSucceed: { (imageData) in
if let image = UIImage(data: imageData) {
self.drinkImage = image
}
onSucceed()//must call completion handler
}) { (error) in
onFailure(error)
}
}
}
//MARK: Tableview Delegates
extension DrinkViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return numberOfRows
return drinksPerPage
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = drinkTableView.dequeueReusableCell(withIdentifier: DRINK_CELL_REUSE_IDENTIFIER) as! DrinkCell
//get image from separate url
if let image = totalDrinksArray[indexPath.row].drinkThumbnailUrl{//index out of range error here
updateImage(imageUrl: image, onSucceed: {
if let currentImage = self.drinkImage{
DispatchQueue.main.async {
cell.drinkImage.image = currentImage
}
}
}, onFailure: { (error) in
print(error)
})
}
cell.drinkLabel.text = totalDrinksArray[indexPath.row].drinkName
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let image = totalDrinksArray[indexPath.row].drinkThumbnailUrl{
updateImage(imageUrl: image, onSucceed: {
}, onFailure: { (error) in
print(error)
})
}
performSegue(withIdentifier: DRINK_SEGUE, sender: indexPath.row)
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastElement = drinksPerPage
if indexPath.row == lastElement {
self.drinkTableView.reloadData()
}
}
}
I saw this post: tableview-loading-more-cell-when-scroll-to-bottom and implemented the willDisplay function but am getting an "index out of range" error.
Can you tell me why you are doing this if you are getting all results at once then you don't have to limit your display since it is automatically managed by tableview. In tableview all the cells are reused so there will be no memory problem. UITableViewCell will be created when it will be shown.
So no need to limit the cell count.
I dont now what you are doing in your code but:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastElement = drinksPerPage // no need to write this line
if indexPath.row == lastElement { // if block will never be executed since indexPath.row is never equal to drinksPerPage.
// As indexPath starts from zero, So its value will never be 2.
self.drinkTableView.reloadData()
}
}
Your app may be crashing because may be you are getting only one item from server.
If you seriously want to load more then you can try this code:
Declare numberOfItem which should be equal to drinksPerPage
var numberOfItem = drinksPerPage
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return numberOfRows
return numberOfItem
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == numberOfItem - 1 {
if self.totalDrinksArray.count > numberOfItem {
let result = self.totalDrinksArray.count - numberOfItem
if result > drinksPerPage {
numberOfItem = numberOfItem + drinksPerPage
}
else {
numberOfItem = result
}
self.drinkTableView.reloadData()
}
}
}

how to implement one radio button to be active while selecting it?

I am having table view and on table view cell the radio button has been placed and the radio buttons may increase or decrease dynamically when I select one radio button the other buttons should not active at a time here is the code I used for selecting a button but I am unable to make other button inactive at a time
func paymentMethodURL() {
let url = NSURL(string: self.paymentmethodURL)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
self.paymentmethodsArray = (jsonObj!.value(forKey: "payment method") as? [[String: AnyObject]])!
OperationQueue.main.addOperation({
self.tableDetails.reloadData()
})
}
}).resume()
}
func paymentreviewURL() {
let url = NSURL(string: self.paymentReviewURL)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
let arrayss = jsonObj?.allKeys
OperationQueue.main.addOperation({
})
}
}).resume()
}
#IBAction func selectRadioButton(_ sender: KGRadioButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
} else{
}
}
func numberOfSections(in tableView: UITableView) -> Int{
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return paymentmethodsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let paymentcell = tableView.dequeueReusableCell(withIdentifier: "cell",for:indexPath) as! paymentTableViewCell
tableDetails.isHidden = false
myActivityIndicator.stopAnimating()
let arr = self.paymentmethodsArray[indexPath.row]
paymentcell.paymentNameLabel.text = arr["name"]as? String
return paymentcell
}
you should like this:
var tagSelected = -1
//assume each radio button have tag 0 1 2
#IBAction func selectRadioButton(_ sender: KGRadioButton) {
tagSelected = sender.tag
tableView.reloadData()
}
//cell for RowAtIndexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier:
kCellReuseIdentifier, for: indexPath) as? YourCell else {
fatalError("Cell is not dequeued")
}
//logic for match indexPath.row /section to your button tags
if indexPath.row == tagSelected {
//selected here your radio button
} else {
//unselect all others
}
//other code
return cell
}
var checkIsRadioSelect = [Int]()
var webserviceArray = [modelObj]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let obj = webserviceArray[indexPath.row]
cell.radioButton.tag = indexPath.row
let checkIndex = self.checkIsRadioSelect.index(of: obj.wateverUniqeId)
if(checkIndex != nil){
cell.radioButton.isSelected = true
}else{
cell.radioButton.isSelected = false
}
}
#IBAction func selectRadioButton(_ sender: UIButton) {
let obj = webserviceArray[sender.tag]
let chekIndex = self.checkIsRadioSelect.index(of:obj.wateverUniqeId)
if sender.isSelected {
self.checkIsRadioSelect.remove(at: sender.tag)
} else{
if(chekIndex == nil){
self.checkIsRadioSelect.append(obj.wateverUniqeId)
}
} self.tableview.reloadData()
}
pretty simple solution is to have an integer for exemple to hold the index of the cell containing the selected button, for exemple at t=0 none is selected so say initial value for this new variable would be -1, once the user select button in cell 3 , u put 3 in that new variable , and later when he select cell 5 first you check if the variable has a valid index in it (in this case 3) then you go unselect the radio in cell 3 (which u have its index) and then select cell 5 and so on and so on..
now in your case I take it that IBAction is inside you cell controller, so you dont have direct access to the tableview and other cells to "deselect" if needed, what I would do is create a delegate protocol for that cell and have the viewcontroller containing the tableview be its delegate so whenever the user select a button i just invoke the delegate method with the cell's index as parameter and obviously implement this delegate method in the viewcontroller and do as I said earlier, here s an exemple on how it would go :
in the cell controller :
#IBAction func selectRadioButton(_ sender: KGRadioButton) {
if sender.isSelected {
} else{
self.delegate?.selectButton(index: self.index)
//invoke the delegate so that the viewcontroller deselect any other cell that might be selected
}
}
and in the viewcontroller :
func selectButton(index: index) {
if self.selectedCell != -1 {
let cell = self.tableView.cellForRowAtIndex(IndexPath(0, self.selectedCell))
cell.selectRadioButton(cell.button)
}
self.selectedCell = index
}

Single table view through two different NSFetchedResultsControllers with sections

Good morning to everyone. I am using Swift 3.1.1, Xcode 8.3.2. I need to connect a single Table View to two different tables (entities) in Core Data through two different NSFetchedResultsControllers. I have created two NSFetchedResultsControllers, and even fetched data from table but I faced problem how to tell Table View that first controller should response for section one and second controller should be responsible for section two.
I can show you the code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var tv: UITableView!
#IBAction func pressed(_ sender: UIButton) {
ModelA.read { table1 in
ModelB.read { table2 in
if table1.isEmpty {
ModelA.save(recordToSave: [(a: 1, b: "a"), (a: 2, b: "b"), (a: 3, b: "c")]) {
ModelB.save(recordToSave: [(a: 4, b: 5.0, c: true), (a: 6, b: 7.0, c: false)]) {
self.tvReload()
}
}
} else {
self.tvReload()
}
}
}
}
var fetchedResultsControllerForModelA = CoreDataFunctions.fetchedResultsController
var fetchedResultsControllerForModelB = CoreDataFunctions.fetchedResultsController
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tvReload()
}
func modelOfTableA(indexPath: IndexPath) -> (forLabel1: String, forLabel2: String, forLabel3: String)? {
if let fetchedResultsControllerForModelA = CoreDataFunctions.getNSManagedObjectForIndexPathOfTable(fetchedResultsController: fetchedResultsControllerForModelA, indexPath: indexPath) {
if let model = ModelA.read(nsmanagedobject: fetchedResultsControllerForModelA) {
return (forLabel1: "\(model.a)", forLabel2: model.b, forLabel3: "")
}
}
return nil
}
func modelOfTableB(indexPath: IndexPath) -> (forLabel1: String, forLabel2: String, forLabel3: String)? {
if let fetchedResultsControllerForModelB = CoreDataFunctions.getNSManagedObjectForIndexPathOfTable(fetchedResultsController: fetchedResultsControllerForModelB, indexPath: indexPath) {
if let model = ModelB.read(nsmanagedobject: fetchedResultsControllerForModelB) {
return (forLabel1: "\(model.a)", forLabel2: "\(model.b)", forLabel3: "\(model.c)")
}
}
return nil
}
func tvReload() {
fetchedResultsControllerForModelA = CoreDataFunctions(tableName: .a).fetchedResultsController(keyForSort: ModelA.a.rawValue, searchParameters: nil)
fetchedResultsControllerForModelB = CoreDataFunctions(tableName: .b).fetchedResultsController(keyForSort: ModelB.a.rawValue, searchParameters: nil)
do {
try fetchedResultsControllerForModelA?.performFetch()
try fetchedResultsControllerForModelB?.performFetch()
DispatchQueue.main.async {
self.tv.reloadData()
}
} catch {
print("Error")
}
}
func numberOfSectionsInTableView(_ tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections1 = fetchedResultsControllerForModelA?.sections {
if let sections2 = fetchedResultsControllerForModelB?.sections {
return sections1[section].numberOfObjects + sections2[section].numberOfObjects
}
return sections1[section].numberOfObjects
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
if indexPath.section == 0 {
if let modelOfTable = modelOfTableA(indexPath: indexPath) {
cell.l1.text = modelOfTable.forLabel1
cell.l2.text = modelOfTable.forLabel2
cell.l3.text = modelOfTable.forLabel3
}
} else {
if let modelOfTable = modelOfTableB(indexPath: indexPath) {
cell.l1.text = modelOfTable.forLabel1
cell.l2.text = modelOfTable.forLabel2
cell.l3.text = modelOfTable.forLabel3
}
}
return cell
}
}
I could not find any tutorial on this theme, so I am asking question there. I do not want to use inheritance from single entity in Core Data, because, in real life it would be impossible.
Thank you for any help or advice!
OK - I downloaded your code, and there are a couple issues...
1) If you want your data to fill two sections in the table, the table must have two sections - currently, you are just returning 1, so use this (although you may want/need different handling based on data retrieved):
func numberOfSectionsInTableView(_ tableView: UITableView) -> Int {
if fetchedResultsControllerForModelA == nil || fetchedResultsControllerForModelB == nil {
return 0
}
return 2
}
2) For number of rows in each section, your code was close but not quite right...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if let sections = fetchedResultsControllerForModelA?.sections {
return sections[0].numberOfObjects
}
} else {
if let sections = fetchedResultsControllerForModelB?.sections {
return sections[0].numberOfObjects
}
}
return 0
}
3) For the actual cellForRowAtIndexPath data, again your code was close but you need to keep track of which data to get for each section...
func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
if indexPath.section == 0 {
if let modelOfTable = modelOfTableA(indexPath: indexPath) {
cell.l1.text = modelOfTable.forLabel1
cell.l2.text = modelOfTable.forLabel2
cell.l3.text = modelOfTable.forLabel3
}
} else {
// Each "Table data model" has only one section... since this is the 2nd table section, we need
// to change the section of the index path for our call to the data model
let dataIndexPath = IndexPath(row: indexPath.row, section: 0)
if let modelOfTable = modelOfTableB(indexPath: dataIndexPath) {
cell.l1.text = modelOfTable.forLabel1
cell.l2.text = modelOfTable.forLabel2
cell.l3.text = modelOfTable.forLabel3
}
}
return cell
}
That should get you on your way.

Resources