My problem is that my segmented control won't pass information to my cellForRowAtIndexPath method in my tableView. When each tab is selected, I wanted to my table view to display different data.
Here is a snippet of my code from where I think my problem originates:
var parsedDataForTableView : ParseForDetailView? {
didSet {
self.selectedIndex = (parsedDataForTableView?.tabSelected)!
self.tableView.rowHeight = (parsedDataForTableView?.tableViewRowHeight)!
self.tableView.reloadData()
}
}
var selectedIndex = Int()
override func viewDidLoad() {
super.viewDidLoad()
self.parsedDataForTableView = ParseForDetailView(segmentedTabSelected: 0, primaryBarData: self.primaryBarDetails!, secondaryBarData: self.secondaryBarDetails!)
configureView()
configureNavigationBarItems()
setImageShadow()
}
#IBAction func switchBetweenTabs(sender: AnyObject) {
if segmentControl.selectedSegmentIndex == 0 {
print("Drinks Selected")
self.selectedIndex = 0
print(self.selectedIndex)
self.parsedDataForTableView = ParseForDetailView(segmentedTabSelected: 0, primaryBarData: self.primaryBarDetails!, secondaryBarData: self.secondaryBarDetails!)
self.tableView.reloadData()
}
if segmentControl.selectedSegmentIndex == 1 {
print("Entertainment Selected")
self.selectedIndex = 1
print(self.selectedIndex)
self.parsedDataForTableView = ParseForDetailView(segmentedTabSelected: 1, primaryBarData: self.primaryBarDetails!, secondaryBarData: self.secondaryBarDetails!)
self.tableView.reloadData()
}
if segmentControl.selectedSegmentIndex == 2 {
print("Info Selected")
self.selectedIndex = 2
print(self.selectedIndex)
self.parsedDataForTableView = ParseForDetailView(segmentedTabSelected: 2, primaryBarData: self.primaryBarDetails!, secondaryBarData: self.secondaryBarDetails!)
self.tableView.reloadData()
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("detailViewCell") as! DetailViewCell
print(self.selectedIndex)
if self.selectedIndex == 0 {
cell.drinkLabel.text = self.parsedDataForTableView?.drinksArray[indexPath.section][indexPath.row]
cell.priceLabel.text = self.parsedDataForTableView?.priceArray[indexPath.section][indexPath.row]
cell.entertainmentLabel.text = ""
} else if self.selectedIndex == 1 {
cell.entertainmentLabel.text = self.parsedDataForTableView?.entertainmentString
cell.drinkLabel.text = ""
cell.priceLabel.text = ""
} else if self.selectedIndex == 2 {
// finish this...
cell.drinkLabel.text = "Info"
cell.priceLabel.text = "Info"
cell.entertainmentLabel.text = "Info"
}
return cell
}
So above we have my IBAction connected to my segmented tab and my cellForRowAtIndexPath method. I want to pass the selectedIndex property into the cellForRowAtIndexPath method to switch between the different data I want to display - initialized in my didSet up top.
When I run the code the view loads just fine with the first segment's information (0th segment index) displaying correctly and my print(self.selectedIndex) within my cellForRowAtIndexPath prints "0" for every cell printed as planned. When I tap the second and third segmented index that same print function prints nothing.
When I print the information inside my IBAction to the console everything is updating correctly.
Why does only my first index register with the cellForRowAtIndexPath method?
I may have mislead you to believe my problem lied somewhere in my logic. My problem was that in my numberOfRowsInSection I didn't account for all the three segmented tab cases therefore my data could not be displayed properly in the table view.
Thank you #NoName
Related
When a cell is deleted, the item at the end of the list takes the place of the item that just got deleted. This only happens when there are more than 3 items in the list.
In the gif below I delete numbers 3 and 4 which leaves me with numbers 1,2,5 in the simulator. HOWEVER in the Realm file I have numbers 1,2,4. I have no clue why it does this?
Data Model
import Foundation
import RealmSwift
class Item: Object {
#objc dynamic var name = ""
}
View Controller
import UIKit
import RealmSwift
class ListViewController: UITableViewController {
let realm = try! Realm()
var itemArray : Results<Item>?
var item:Item?
override func viewDidLoad() {
super.viewDidLoad()
self.itemArray = realm.objects(Item.self)
}
#IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add New Item", message: "", preferredStyle: .alert)
alert.view.tintColor = UIColor.red
let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
let newItem = Item()
newItem.name = textField.text!
try! self.realm.write {
self.realm.add(newItem)
}
self.tableView.reloadData()
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Create new item"
textField = alertTextField
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.itemArray!.count//Size of the Array
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)//Asigns the Protocol Cell
let data = self.itemArray![indexPath.row]
cell.textLabel?.text = data.name
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
if let item = itemArray?[indexPath.row] {
try! self.realm.write {
self.realm.delete(item)
}
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
}
You are retrieving an unsorted result set from Realm, which as per documentation does not necessarily retain insertion order after deletions (basically when you remove 3, then 5 is shifted in its place):
Note that the order of Results is only guaranteed to stay consistent when the query is sorted. For performance reasons, insertion order is not guaranteed to be preserved.
So there are two things you can do:
1.) sort the result set
2.) instead of assuming you're only deleting a single object and otherwise have no movements of any sort, you can rely on Realm's own diffing + change set evaluation with a notification token so that you receive a change set for any possible change that happens to the result set.
// see https://realm.io/docs/swift/latest/#collection-notifications
class ViewController: UITableViewController {
var notificationToken: NotificationToken? = nil
override func viewDidLoad() {
super.viewDidLoad()
let realm = try! Realm()
let results = realm.objects(Person.self).filter("age > 5")
// Observe Results Notifications
notificationToken = results.observe { [weak self] (changes: RealmCollectionChange) in
guard let tableView = self?.tableView else { return }
switch changes {
case .initial:
// Results are now populated and can be accessed without blocking the UI
tableView.reloadData()
case .update(_, let deletions, let insertions, let modifications):
// Query results have changed, so apply them to the UITableView
tableView.beginUpdates()
tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
with: .automatic)
tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
tableView.endUpdates()
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError("\(error)")
}
}
}
deinit {
notificationToken?.invalidate()
}
}
I have one table view using a different cell based on what tab you select. It is working, although the 1st one seems to be controlling the height. I have one row in the first tab but 2 in the second and both just show 1.
I'm wondering if I need to clear dequeueReusableCell on tab select or something like that. Any Ideas?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if activeTab == "tab1" {
return someService.tab1.count
} else {
return someService.tab2.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if activeTab == "tab1" {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Tab1Cell") as? Tab1Cell {
let tab1 = someService.tab1[indexPath.row]
cell.updateView(tab1: tab1)
return cell
} else {
return Tab1Cell()
}
} else {
if let cell = tableView.dequeueReusableCell(withIdentifier: "Tab2Cell") as? Tab2Cell {
let tab2 = someService.tab2[indexPath.row]
cell.updateView(tab2: tab2)
return cell
} else {
return Tab2Cell()
}
}
}
I'm also fixing the scroll with this:
func fixTableViewSize() {
let cells = self.notesTable.visibleCells
var heightOfTableView = 0
for cell in cells {
print("cell")
heightOfTableView += Int(cell.frame.height)
}
self.tableViewHeightConstraint.constant = CGFloat(heightOfTableView)
self.buttonFromTableTopConstraint.constant = CGFloat(heightOfTableView)
}
I have researched this and nothing seems to be working. I am trying to build a recipe app and the the image of the dish & names of the dish (appetizer) are not downloading in order. How can I do this?
Code:
class Appetizers: UITableViewController {
var valueToPass: String!
var valuePassed: String!
var appetizer = [String]()
var images = [UIImage]()
func refresh() {
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
override func viewDidLoad() {
super.viewDidLoad()
// Parse - class - column
let query = PFQuery(className: "Appetizers")
query.orderByAscending("appetizer")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error == nil {
if let objects = objects {
for object in objects {
let load = object.objectForKey("appetizer") as! String
self.appetizer.append(load)
let imageFile = object["imageFiles"] as! PFFile
imageFile.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
if error != nil {
print(error)
} else {
if let data = imageData {
self.images.append(UIImage(data: data)!)
self.tableView.reloadData()
}
}
})
self.tableView.reloadData()
}
}
} else {
print("Error: \(error!) \(error!.userInfo)")
}
}
sleep(1)
refresh()
}
override func viewWillAppear(animated: Bool) {
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return appetizer.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = appetizer[indexPath.row]
// add image to table
if images.count > indexPath.row {
cell.imageView?.image = images[indexPath.row]
}
return cell
}
// when user taps on cell ...
func getCellLabel () {
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
valueToPass = currentCell.textLabel!.text
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
getCellLabel()
self.performSegueWithIdentifier("0", sender: self)
}
}
When performing asynchronous queries, you have no assurances regarding the order they complete. So, the concept of two separate arrays, one an array of strings and another an array of images will always be problematic.
You could, for example, replace images with a dictionary indexed by the appetizer name, and thus it wouldn't matter what order they complete.
var appetizer = [String]()
var images = [String: UIImage]()
Thus, it might look like:
override func viewDidLoad() {
super.viewDidLoad()
let query = PFQuery(className: "Appetizers")
query.orderByAscending("appetizer")
query.findObjectsInBackgroundWithBlock { objects, error in
guard error == nil, let objects = objects else {
print(error)
return
}
for (index, object) in objects.enumerate() {
let appetizerName = object.objectForKey("appetizer") as! String
self.appetizer.append(appetizerName)
let imageFile = object["imageFiles"] as! PFFile
imageFile.getDataInBackgroundWithBlock { imageData, error in
guard error == nil, let data = imageData else {
print(error)
return
}
// when the image comes in, asynchronously update only that one row
self.images[appetizerName] = UIImage(data: data)
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: 0)], withRowAnimation: .Fade)
}
}
// reload the table only once, after all of the `appetizer` entries are created (but likely before the images come in)
self.tableView.reloadData()
}
}
And
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let name = appetizer[indexPath.row]
cell.textLabel?.text = name
cell.imageView?.image = images[name]
return cell
}
Or you could just replace these two separate properties with one that is an array of custom objects (e.g. an Appetizer object that has both a name property and an image property).
But any way you do that, you want to name sure you're not dealing with two separate array.
By the way, but this process of loading all of the images can be problematic if you have a lot of rows. This code is employing "eager" loading of images (loading them whether they're currently required or not). The problem is that images are relatively large assets (in comparison to the string values) and you can run into memory issues, network bandwidth issues, etc.
One generally likes to employ "lazy" loading (e.g. let cellForRowAtIndexPath request the image only when it's needed. For example, let's say you have 200 rows, of which only 12 are visible at one point. You shouldn't be requesting 200 images, but rather only that for the 12 visible ones. If you take the image retrieval out of viewDidLoad and, instead, have cellForRowAtIndexPath request them one at a time, you'll have much better network performance and less demanding memory characteristics.
If you're going to save the images in some structure like the code currently does, at the very least make sure you purge those images upon receiving notification of a memory warning (and, obviously, gracefully handle the re-requesting them in a JIT manner as needed).
I figured out the problem with my table not loading without sleep() ...
I had 'self.tableView.reloadData()' outside of the block.
Rob was very helpful :)
Assignment: "Create an application that displays a list of gas stations and their gas prices and distance. Store the information into a Dictionary. Display the results in a table view. Let the user select an entry and then display a UIAlert dialog showing the entry."
I am working on the dictionary part. The code for the dictionary is
"var gasStation = ["76": ["$2.76", "1.2 miles"],
"Arco":["$2.56", "2.4 miles"],
"Shell":["$3.54", "3.5 miles"],
"Tower mart": ["$2.36", "5.7 miles"]]"
The error pops up on this line of code
cell!.textLabel!.text = gasStation[indexPath.row]
Here is the New Updated code
import UIKit
class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
let dwarves = [ "Sleepy", "Sneezy", "bashful", "Happy"]
let gasStation = ["76": ["$2.76", "1.2 miles"],
"Arco":["$2.56", "2.4 miles"],
"Shell":["$3.54", "3.5 miles"],
"Tower-Mart": ["$2.36", "5.7 miles"]]
var gasStationNames = Array(gasStation) // error: "ViewController.Type' does not have a member named 'gasStation'
let simpleTableIdentifier = "SimpleTableIdentifier"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return dwarves.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(
simpleTableIdentifier) as? UITableViewCell
if (cell == nil) {
cell = UITableViewCell(
style: UITableViewCellStyle.Default, reuseIdentifier: simpleTableIdentifier)
}
cell!.textLabel?.text = dwarves[indexPath.row]
return cell!
}
}
This is how you get an array of all the gas Station names.
let gasStationNames = Array(gasStation.keys)
But it will not be in the same order.
And for assigning it to the tableView:
cell!.textLabel!.text = gasStationNames[indexPath.row]
Here, you can index gasStationNames with indexPath.row of type Int as it is of type Array. And to get a value out of Dictionary, you should pass a key of proper type.
You should have a look at this document page
EDIT
Assign gasStationNames in your cellForRow
let gasStationNames = Array(gasStation.keys)
and then assign to cell's textLabel
cell!.textLabel!.text = gasStationNames[indexPath.row]
This is the previous screen.
Then, I clicked the search bar:
Code is below:
// MARK: - searchController
func initSearchController() {
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.backgroundColor = UIColor.clearColor()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
}
Please help me. Thanks.
Set the title of the section to nil, if there are no rows in that section, this would hide the section title
// Set the title of the section header, return nil if no rows in that section
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if self.tableView(tableView, numberOfRowsInSection: section) == 0 {
return nil
} else {
return "section title \(section)"
}
}
This will work, assuming tableView(tableView: UITableView, numberOfRowsInSection section: Int) is implemented properly to reflect the number of sections based on the search result.
If you want to remove the section title altogether when the search bar is active, then just add this
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if self.resultSearchController.active {
return nil
} else {
return "section title \(section)"
}
}