I’m trying to have a custom cell whom’s variable assignment changes based on if and else-if statements after the variable is declared.
This is to have the custom cell, which shows business hours of a selected business, be able to pick the custom table view cell that fits with the amount of open and closed time range pairs that I would like to show for the week for that business (as some businesses have more than one in one day).
However, I keep getting an error message saying:
“Cannot assign value of type 'DetailsHoursContentsForAtLeastOneDayWithTwoOpenCloseTimeRangePairsTableViewCell' to type ‘DetailsHoursContentsForAllDaysWithOneOpenCloseTimeRangePairTableViewCell'”
at this line of code:
detailsHoursContentsCustomCell = scrollableCitiesRestaurantDetailsTableView.dequeueReusableCell(withIdentifier: "DetailsHoursContentsForAtLeastOneDayWithTwoOpenCloseTimeRangePairsTableViewCell", for: indexPath) as! DetailsHoursContentsForAtLeastOneDayWithTwoOpenCloseTimeRangePairsTableViewCell
which is the line of code within the conditional statement of the first else-if statement, which changes the variable assignment of the custom cell variable I’m using. This line of code has a comment above it with the error message it got in the code snippet further below in this post. The conditional statement in the if-statement before this else-if statement, sets the value as the same value that the custom cell variable starts out as, and doesn’t get this type of error message, or any type of error message in it’s conditional statement.
I also get this same error message in the other else-if statements’ conditional statements, where the custom cell variable value is changed. There are also comments above these lines of code saying the error message that was received for that particular line of code in the code snippet further below.
What I’ve tried:
-Investigated whether using “as?” or “as” instead of force casting (using “as!”) in the lines of code where I’m getting these same types of error messages, might work, but it seemed to me like it wouldn’t.
How can I fix this error message, and/or how can I go about doing what I’m trying to do if the way I’m currently trying to do it won’t work (with the “correct” code being used)?
Thanks!
The code is below.
Code:
ViewController.swift:
import UIKit
//*Some code for a view controller for a table view file.*
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
//*Code for this cell goes here.*
return custumCell
}
//*More "indexPath.row" code goes here for other table view cells being used.*
if indexPath.row == 10 {
var detailsHoursContentsCustomCell = scrollableCitiesRestaurantDetailsTableView.dequeueReusableCell(withIdentifier: "DetailsHoursContentsForAllDaysWithOneOpenCloseTimeRangePairTableViewCell", for: indexPath) as! DetailsHoursContentsForAllDaysWithOneOpenCloseTimeRangePairTableViewCell
//If number of open hour time rnage pairs for all days is one.
if selectedVenueDetailViewInfo.hours.numberOfOpenHoursTimeRangesAsStringTypeOfTableViewCellToUse == "OneTimeRange" {
detailsHoursContentsCustomCell = scrollableCitiesRestaurantDetailsTableView.dequeueReusableCell(withIdentifier: "DetailsHoursContentsForAllDaysWithOneOpenCloseTimeRangePairTableViewCell", for: indexPath) as! DetailsHoursContentsForAllDaysWithOneOpenCloseTimeRangePairTableViewCell
}
//Else If number of open hour time rnage pairs for at least one day is two.
else if selectedVenueDetailViewInfo.hours.numberOfOpenHoursTimeRangesAsStringTypeOfTableViewCellToUse == "TwoTimeRanges" {
//Below is the line of code where I get the error message: "Cannot assign value of type 'DetailsHoursContentsForAtLeastOneDayWithTwoOpenCloseTimeRangePairsTableViewCell' to type 'DetailsHoursContentsForAllDaysWithOneOpenCloseTimeRangePairTableViewCell'".
detailsHoursContentsCustomCell = scrollableCitiesRestaurantDetailsTableView.dequeueReusableCell(withIdentifier: "DetailsHoursContentsForAtLeastOneDayWithTwoOpenCloseTimeRangePairsTableViewCell", for: indexPath) as! DetailsHoursContentsForAtLeastOneDayWithTwoOpenCloseTimeRangePairsTableViewCell
}
//Else If number of open hour time rnage pairs for at least one day is three.
else if selectedVenueDetailViewInfo.hours.numberOfOpenHoursTimeRangesAsStringTypeOfTableViewCellToUse == "ThreeTimeRanges" {
//Below is the line of code where I get the error message: "Cannot assign value of type 'DetailsHoursContentsForAtLeastOneDayWithThreeOpenCloseTimeRangePairsTableViewCell' to type 'DetailsHoursContentsForAllDaysWithOneOpenCloseTimeRangePairTableViewCell'".
detailsHoursContentsCustomCell = scrollableCitiesRestaurantDetailsTableView.dequeueReusableCell(withIdentifier: "DetailsHoursContentsForAtLeastOneDayWithThreeOpenCloseTimeRangePairsTableViewCell", for: indexPath) as! DetailsHoursContentsForAtLeastOneDayWithThreeOpenCloseTimeRangePairsTableViewCell
}
//Else If number of open hour time rnage pairs for at least one day is more than three.
else if selectedVenueDetailViewInfo.hours.numberOfOpenHoursTimeRangesAsStringTypeOfTableViewCellToUse == "GreaterThanThreeTimeRanges" {
//Below is the line of code where I get the error message: "Cannot assign value of type 'DetailsHoursContentsForAtLeastOneDayWithGreaterThanThreeOpenCloseTimeRangePairsTableViewCell' to type 'DetailsHoursContentsForAllDaysWithOneOpenCloseTimeRangePairTableViewCell'".
detailsHoursContentsCustomCell = scrollableCitiesRestaurantDetailsTableView.dequeueReusableCell(withIdentifier: "DetailsHoursContentsForAtLeastOneDayWithGreaterThanThreeOpenCloseTimeRangePairsTableViewCell", for: indexPath) as! DetailsHoursContentsForAtLeastOneDayWithGreaterThanThreeOpenCloseTimeRangePairsTableViewCell
}
//Setting hours as labels.
detailsHoursContentsCustomCell.sundayHoursOpenWithoutDayLabel.text = selectedVenueDetailViewInfo.hours.sundayOpenHoursWithoutDay
detailsHoursContentsCustomCell.mondayHoursOpenWithoutDayLabel.text = selectedVenueDetailViewInfo.hours.mondayOpenHoursWithoutDay
detailsHoursContentsCustomCell.tuesdayHoursOpenWithoutDayLabel.text = selectedVenueDetailViewInfo.hours.tuesdayOpenHoursWithoutDay
detailsHoursContentsCustomCell.wednesdayHoursOpenWithoutDayLabel.text = selectedVenueDetailViewInfo.hours.wednesdayOpenHoursWithoutDay
detailsHoursContentsCustomCell.thursdayHoursOpenWithoutDayLabel.text = selectedVenueDetailViewInfo.hours.thursdayOpenHoursWithoutDay
detailsHoursContentsCustomCell.fridayHoursOpenWithoutDayLabel.text = selectedVenueDetailViewInfo.hours.fridayOpenHoursWithoutDay
detailsHoursContentsCustomCell.saturdayHoursOpenWithoutDayLabel.text = selectedVenueDetailViewInfo.hours.saturdayOpenHoursWithoutDay
return detailsHoursContentsCustomCell
}
}
//*Some Extensions.*
The part of the model used for the business hours data that is shown in the detail view, after a restaurant is selected:
DetailViewModel.swift:
import Foundation
//*Other code for getting data such as name of restaurant, street address of restaurant, etc. for the detail view not using Codable. Am using NSDictionary.*
struct OpenHoursForDaysOfWeek {
var mondayOpenHoursWithoutDay: String
var tuesdayOpenHoursWithoutDay: String
var wednesdayOpenHoursWithoutDay: String
var thursdayOpenHoursWithoutDay: String
var fridayOpenHoursWithoutDay: String
var saturdayOpenHoursWithoutDay: String
var sundayOpenHoursWithoutDay: String
var numberOfOpenHoursTimeRangesAsStringTypeOfTableViewCellToUse: String
}
My Table View Cell Layouts:
AllDaysWithOneTimeRangeTableViewCell
Without seeing your UI design, I'm assuming:
It is likely you could use a single cell design, and then show/hide the different UI elements as needed.
For example:
That image shows 3 different prototype cells.
However, the bottom cell has 3 "rows" in a vertical
UIStackView. We could easily turn that into the first cell by hiding the 2nd and 3rd "row" and setting the yellow and cyan label text.If you feel your layout / design is too complex, and you want to stick with multiple cell prototypes, then I'd suggest having the cell classes handle parsing the data...
So each cell could have a function like this:
Presumably, each cell type would have different labels.text to set.
Then your
cellForRowAtlooks like this:Edit - after comments and seeing the layout goal...
Now that I've seen your layout, this is a simpler task. It can be done with a single cell class, and no need to show/hide elements at all.
Two options:
Each has its benefits and drawbacks... using an attributed string in a single label would be nice:
except --- there are differences in attributed strings between iOS versions. Somebody who is more familar with it might know how to work around that.
Using stack views is pretty straightforward.
Here's how it would look with yellow backgrounds so we can see the label framing:
and without the yellow:
I don't know how you are getting your data, so I made a
Venuestruct that looks like this:Example
hoursArrays - when filled with data:So, here's a sample cell class - all via code, so no Storyboard or
@IBOutletconnections:a simple sample data class to generate some sample values:
and a sample view controller: