How to conditionally section data model in UITableView? - swift

I have a model object being returned in JSON from Firebase.
{
"id": 1,
"name": "Jon Doe",
"time": ["1525592246"]
},
{
"id": 2,
"name": "Jane Doe",
"time": ["1525592266"]
},
I would like to structure these objects into sections in a UITableView based on the below:
enum DiarySectionType {
case Today
case Tomorrow
case ThisWeek
case ThisMonth
case Later
}
I.e If a "time" is today it will be in the today section of the UITableView
What is the best way to approach this? I've thought of having separate arrays for each. But don't think that's the way to go.
As always any help appreciated.

First you need helper extension for your Date
extension Date {
public func component(_ component: Calendar.Component) -> Int {
let calendar = Calendar.autoupdatingCurrent
return calendar.component(component, from: self)
}
public var isToday: Bool {
let calendar = Calendar.autoupdatingCurrent
return calendar.isDateInToday(self)
}
public var isTomorrow: Bool {
let calendar = Calendar.autoupdatingCurrent
return calendar.isDateInTomorrow(self)
}
public var isThisWeek: Bool {
return isInSameWeek(date: Date())
}
public var isThisMonth: Bool {
return isInSameMonth(date: Date())
}
public var islater: Bool {
return isInSameMonth(date: Date()) ? false : true
}
}
extension Date {
func isInSameWeek(date: Date) -> Bool {
return Calendar.current.isDate(self, equalTo: date, toGranularity: .weekOfYear)
}
func isInSameMonth(date: Date) -> Bool {
return Calendar.current.isDate(self, equalTo: date, toGranularity: .month)
}
}
Then your Enum
enum DiarySectionType:Int{
case Today
case Tomorrow
case ThisWeek
case ThisMonth
case Later
case count // use for number of section
init (with date:Date){
if date.isToday {
self = .Today
}else if date.isTomorrow{
self = .Tomorrow
}else if date.isThisWeek{
self = .ThisWeek
}else if date.isThisMonth{
self = .ThisMonth
}else{
self = .Later
}
}
}
Your data Formalize as you need
struct Item{
var id : Int
var name : String
var time : Double
init(id:Int, name:String,time:Double) {
self.id = id
self.name = name
self.time = time
}
}
// Controller
class ViewController: UIViewController{
var data = [Item]() // your data before sections
var dataWork = [DiarySectionType: [Item]]() // date After sections
override func viewDidLoad() {
super.viewDidLoad()
self.sortData()
}
// When generating sorted table data we can easily use our TableSection to make look up simple and easy to read.
func sortData() {
dataWork[.Today] = data.filter({ DiarySectionType.init(with: Date.init(timeIntervalSince1970: $0.time)) == .Today })
dataWork[.Tomorrow] = data.filter({ DiarySectionType.init(with: Date.init(timeIntervalSince1970: $0.time)) == .Tomorrow })
dataWork[.ThisWeek] = data.filter({ DiarySectionType.init(with: Date.init(timeIntervalSince1970: $0.time)) == .ThisWeek })
dataWork[.ThisMonth] = data.filter({ DiarySectionType.init(with: Date.init(timeIntervalSince1970: $0.time)) == .ThisMonth })
dataWork[.Later] = data.filter({ DiarySectionType.init(with: Date.init(timeIntervalSince1970: $0.time)) == .Later })
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
// As long as `count` is the last case in our TableSection enum,
// this method will always be dynamically correct no mater how many table sections we add or remove.
func numberOfSections(in tableView: UITableView) -> Int {
return DiarySectionType.count.rawValue
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Using Swift's optional lookup we first check if there is a valid section of table.
// Then we check that for the section there is data that goes with.
if let tableSection = DiarySectionType(rawValue: section), let data = dataWork[tableSection] {
return data.count
}
return 0
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
var title : String = ""
if let tableSection = DiarySectionType(rawValue: section) {
switch tableSection {
case .Today:
title = "Today"
case .Tomorrow:
title = "Tomorrow"
case .ThisMonth:
title = "ThisMonth"
case .ThisWeek:
title = "ThisWeek"
case .Later:
title = "Later"
default:
title = ""
}
}
return title
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// Similar to above, first check if there is a valid section of table.
// Then we check that for the section there is a row.
if let tableSection = DiarySectionType(rawValue: indexPath.section), let item = dataWork[tableSection]?[indexPath.row] {
// use item item
}
return cell
}
}

Related

swift Diffable data source error - 'Fatal: supplied item identifiers are not unique. Duplicate identifiers:

I think there is a problem with the calendar model or the data I put in the pile, but I can't find it no matter how much I look for it. I'm desperate for help 🥲
The contents of the error are as follows
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Fatal: supplied item identifiers are not unique. Duplicate identifiers: {(
HomeDataSource.Item.calendar
class HomeDataSource {
typealias DataSource = UITableViewDiffableDataSource<Section, Item>
private let tableView: UITableView
private lazy var dataSource = createDataSource()
private let postType: PostCase
var calendars: [Calendar] = [
Calendar(date: "haha"),
Calendar(date: "hhohoho"),
]
private var posts: [UserPost]
enum Section: CaseIterable {
case calendar
case post
init(rawValue: Int) {
switch rawValue {
case 0: self = .calendar
case 1: self = .post
default:
fatalError("not exist section")
}
}
}
enum Item: Hashable {
case calendar
case post
}
init(tableView: UITableView, postType: PostCase) {
self.tableView = tableView
self.postType = postType
self.posts = .init()
}
func createDataSource() -> UITableViewDiffableDataSource<Section, Item> {
tableView.register(CalendarTableViewCell.self, forCellReuseIdentifier: CalendarTableViewCell.identifier)
tableView.register(UserPostTableViewCell.self, forCellReuseIdentifier: UserPostTableViewCell.identifier)
tableView.register(EmptyPostTableViewCell.self, forCellReuseIdentifier: EmptyPostTableViewCell.identifier)
dataSource = UITableViewDiffableDataSource<Section, Item>(tableView: tableView) {
tableView, indexPath, item in
switch item {
case .calendar:
let cell = tableView.dequeueReusableCell(withIdentifier: CalendarTableViewCell.identifier, for: indexPath)
cell.selectionStyle = .none
return cell
case .post:
switch self.postType {
case .postExist:
guard let cell = tableView.dequeueReusableCell(withIdentifier: UserPostTableViewCell.identifier, for: indexPath) as? UserPostTableViewCell else { return UITableViewCell() }
cell.selectionStyle = .none
cell.configure()
return cell
case .friendPostEmpty:
guard let cell = tableView.dequeueReusableCell(withIdentifier: EmptyPostTableViewCell.identifier, for: indexPath) as? EmptyPostTableViewCell else { return UITableViewCell() }
cell.configure(isFriend: true)
return cell
case .ownerPostEmpty:
guard let cell = tableView.dequeueReusableCell(withIdentifier: EmptyPostTableViewCell.identifier, for: indexPath) as? EmptyPostTableViewCell else { return UITableViewCell() }
cell.configure(isFriend: false)
return cell
}
}
}
return dataSource
}
func updateSnapshot(posts: [UserPost]) {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.calendar, .post])
let calendarIds = calendars.map { _ in Item.calendar }
let postIds = posts.map { _ in Item.post }
snapshot.appendItems(calendarIds, toSection: .calendar)
snapshot.appendItems(postIds, toSection: .post)
dataSource.apply(snapshot, animatingDifferences: true)
}
}
Calendar model is as follows
struct Calendar: Hashable {
let id = UUID()
let date: String
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: Calendar, rhs: Calendar) -> Bool {
lhs.id == rhs.id
}
init(date: String) {
self.date = date
}
}
Also, I would like to ask you additionally what should I do if I want to put only one value in the snapshot data?
The problem is here:
let calendarIds = calendars.map { _ in Item.calendar }
let postIds = posts.map { _ in Item.post }
You just map all the different models into equal Item. Any Calendar with unique id turns into Item.calendar which have no id. All of them are equal.
if you still want to us enum then I suggest you declare it like that:
enum Item {
case calendar(Calendar)
case post(UserPost)
}
extension Item: Hashable {
public static func == (lhs: Item, rhs: Item) -> Bool {
switch (lhs, rhs) {
case (.calendar(let calendar1), .calendar(let calendar2)):
return calendar1.id == calendar2.id
case (.post(let post1), .post(let post2)):
return post1.id == post2.id
default:
return false
}
}
public func hash(into hasher: inout Hasher) {
switch self {
case .calendar(let calendar):
hasher.combine("\(calendar.id)_calendar")
case .post(let post):
hasher.combine("\(post.id)_post")
}
}
}
This should help you.
Moreover, as vadian mentioned, you better not use Calendar. Rename it to UserCalendar for example.

How to get value from the first 3 rows in TableView using swift?

I retrieve data from MySql via PHP file to get users information and scores to load them in a table. I need to get the value of the first 3 users and put them in a Label outside the Table, it is like game leaders list. I attached an image to explain the idea.
Here is the structure code:
import Foundation
protocol HomeModelProtocol: AnyObject {
func itemsDownloaded(items: NSArray)
}
class HomeModel: NSObject, URLSessionDataDelegate {
weak var delegate: HomeModelProtocol!
let urlPath = "https://mywebsite.com/folder/callUserList.php" //this will be changed to the path where service.php lives
func downloadItems() {
let url: URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
let task = defaultSession.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON(data!)
}
}
task.resume()
}
func parseJSON(_ data:Data) {
var jsonResult = NSArray()
do{
jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
} catch let error as NSError {
print(error)
}
var jsonElement = NSDictionary()
let users = NSMutableArray()
for i in 0 ..< jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary
let user = UsersModel()
//the following insures none of the JsonElement values are nil through optional binding
if let name = jsonElement["name"] as? String,
let email = jsonElement["email"] as? String,
let phoneNumber = jsonElement["phone"] as? String,
let userImage = jsonElement["image"] as? String
{
user.name = name
user.email = email
user.phoneNumber = phoneNumber
user.userImage = userImage
}
users.add(user)
}
DispatchQueue.main.async(execute: { () -> Void in
self.delegate.itemsDownloaded(items: users)
})
}
}
Here is the model:
import Foundation
class UsersModel: NSObject {
//properties
var name: String?
var email: String?
var phoneNumber: String?
var userImage: String?
//empty constructor
override init()
{
}
//construct with #name, #address, #latitude, and #longitude parameters
init(name: String, email: String, phoneNumber: String, userImage: String) {
self.name = name
self.email = email
self.phoneNumber = phoneNumber
self.userImage = userImage
}
//prints object's current state
override var description: String {
return "Name: \(String(describing: name)), Email: \(String(describing: email)), Phone Number: \(String(describing: phoneNumber)), User Image: \(String(describing: userImage))"
}
}
Here is the code in the TableView controller:
var feedItems: NSArray = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
let homeModel = HomeModel()
homeModel.delegate = self
homeModel.downloadItems()
}
func itemsDownloaded(items: NSArray) {
feedItems = items
self.listTableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of feed items
return feedItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Retrieve cell
let cellIdentifier: String = "BasicCell"
let myCell: WinnerTableCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! WinnerTableCell
// Get the location to be shown
let item: UsersModel = feedItems[indexPath.row] as! UsersModel
// Get references to labels of cell
myCell.lbTextName!.text = item.name
return myCell
}
The data shows in the Table but I have no idea how to fill the 3 label with the 3 first users from the Table.
How can I get these values from the table and pass it to a label in the same ViewController?
Thanks
When adding this code:
if feedItems.count >= 3 {
lblFirstWinner.text = feedItems[0].name // 1st winner
lblSecondWinner.text = feedItems[1].name // 2nd winner
lblThirdWinner.text = feedItems[2].name // 3rd winner
}
it shows error: Value of type 'Any' has no member 'name'
Change itemsDownloaded method as
func itemsDownloaded(items: NSArray) {
feedItems = items
self.listTableView.reloadData()
for (index, user) in items.enumerated() {
let user = user as! UserModel
switch index {
case 0: // 1st winner
lblFirstWinner.text = user.name
case 1: // 2nd winner
lblSecondWinner.text = user.name
case 2: // 3rd winner
lblThirdWinner.text = user.name
}
}
}
OR
Change your HomeModelProtocol method and feedItems type to [UsersModel]
protocol HomeModelProtocol: AnyObject {
func itemsDownloaded(items: [UsersModel]) // Changed
}
var feedItems =[UsersModel]() // Changed
override func viewDidLoad() {
super.viewDidLoad()
let homeModel = HomeModel()
homeModel.delegate = self
homeModel.downloadItems()
}
func itemsDownloaded(items: [UsersModel]) {
feedItems = items
self.listTableView.reloadData()
if feedItems.count >= 3 {
lblFirstWinner.text = feedItems[0].name // 1st winner
lblSecondWinner.text = feedItems[1].name // 2nd winner
lblThirdWinner.text = feedItems[2].name // 3rd winner
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of feed items
return feedItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Retrieve cell
let cellIdentifier: String = "BasicCell"
let myCell: WinnerTableCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! WinnerTableCell
// Get references to labels of cell
myCell.lbTextName!.text = feedItems[indexPath.row].name // Changed
return myCell
}
Just you need to add a few lines in the below function and your solution will be done.
func itemsDownloaded(items: NSArray) {
feedItems = items
self.listTableView.reloadData()
if feedItems.count >= 3 {
lblFirstWinner.text = feedItems[0].name // 1st winner
lblSecondWinner.text = feedItems[1].name // 2nd winner
lblThirdWinner.text = feedItems[2].name // 3rd winner
}
}
Let me know... is it working for you? and please also refer to #vadian comment on your question.

real- time currency converter Swift

I'm trying to make a simple currency conversion application . here I am parsing json
static func fetchJson(key: String = "USD", completion: #escaping (ExchangeRates) -> ()) {
guard let url = URL(string: "https://open.er-api.com/v6/latest/\(String(describing: key))") else {return}
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
return
}
guard let safeData = data else {return}
do {
let results = try JSONDecoder().decode(ExchangeRates.self, from: safeData)
completion(results)
} catch {
print(error)
}
}.resume()
}
then I get a dictionary looks like:
"rates": {
"USD": 1,
"AED": 3.67,
"AFN": 105,
"ALL": 107.39,
"AMD": 481.52,
"ANG": 1.79,
"AOA": 538.31,
..... etc
this is how the structure in which the data is stored looks like
struct ExchangeRates: Codable {
let rates: [String: Double]
}
in the ViewController I have created an object that has the type of ExchangeRates struct and call the func fetchJson in viewDidLoad and save the result in finalCurrencies
var finalCurrencies: ExchangeRates?
then I created a tableview with the number of cells equal to
finalCurrencies.rates.count
tableViewCell has a currencyLabel with currency name (finalCurrencies.rates.key) and currencyTextField with currency value (finalCurrencies.rates.value)
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let currencyFetched = finalCurrencies {
return currencyFetched.rates.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell {
if let currencyFetched = finalCurrencies {
cell.currencyLabel.text = Array(currencyFetched.rates.keys)[indexPath.row]
cell.currencyTextField.accessibilityIdentifier = Array(currencyFetched.rates.keys)[indexPath.row]
let selectedRate = Array(currencyFetched.rates.values)[indexPath.row]
cell.currencyTextField.text = "\(selectedRate)"
return cell
}
}
return UITableViewCell()
}
}
And that's what I get in the end as a result enter image description here
QUESTION: I need to make it so that when the value in the textfield for a certain currency changes, the values of the other currencies change in real- time. as a live google currency converter. I should also be able to switch between currencies and change their values, while also the values of other currencies should change. also how to make it so that with each new number an existing dictionary finalCurrencies is used and not a func fetchJson ?
I suppose that I should use this func textField (so that the textField reacts to each addition of a number).
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
}
I'm a beginner and I've been thinking about this for a few days now and I don't know how to do it.thanks in advance.
First you need to calculate the ratio of the change for the currency which had its value modified, you'll then just map the values of the dictionary with that ratio:
extension Dictionary where Key == String, Value == Double {
mutating func applyChange(of currency: Key, with newValue: Value) {
guard
newValue != .zero,
let oldValue = self[currency],
oldValue != newValue,
oldValue != .zero
else { return }
let ratio = 1.0 + ((newValue - oldValue) / oldValue)
indices.forEach { values[$0] *= ratio }
}
}
If I've understood you correctly, I believe this would work.
Each time the user adjusts the value in the textField you'd loop through your rates dictionary, calculate the new rate and return an updated dictionary.
If I've misunderstood your goal please let me know and I'll do my best to assist :)
struct Converter {
let exchangeRates: [String: Double] = [
"USD": 1,
"AED": 3.67,
"AFN": 105,
"ALL": 107.39,
"AMD": 481.52,
"ANG": 1.79,
"AOA": 538.31
]
func convert(_ amount: String) -> [String: Double] {
var newRates = [String: Double]()
exchangeRates.forEach { (code, value) in
if let amount = Double(amount) {
newRates[code] = value * amount
}
}
return newRates
}
}
let converter = Converter()
let userInput = "10.00"
print(converter.convert(userInput))

swift Lost data from the realm database in another country

I use the realm database for my application (to-do list), everything works fine, BUT once I flew to another country and noticed that the records in the database are empty (the application gives out an empty list), upon arrival back to my country everything returned to normal ... Now I am again in a different country and the situation repeats again (database is empty), for some reason the database gives an empty list result, can you please explain why this is happening and how to fix that?
Output example
var dbToDoList = DBrealmToDoList()
var arrayToDoList: Results<RealmToDoList> {
get {
return dbToDoList.getArray()
}
}
override func viewDidLoad() {
super.viewDidLoad()
let realm = try! Realm()
dbToDoList.realm = realm
let current = arrayToDoList.filter { (_todo) -> Bool in
return _todo.date == date
}.first
self.selectedDate = date
if current != nil {
self.selectedLists = current?.lists
self.selectedListsSorted = self.selectedLists?.sorted(by: { (val, val2) -> Bool in
return (!val.value && val2.value)
})
}
}
And then in tableView I display the data from the selectedListsSorted
// MARK: UITableView
extension ToDoListViewController : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return selectedListsSorted?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ToDoListTableViewCell
let current = selectedListsSorted?[indexPath.row]
cell.nameLabel.text = current?.key
cell.checkBox.isSelected = current?.value ?? false
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 90
}
}
Here is a class for working with db
class RealmToDoList: Object {
#objc private dynamic var dictionaryData: Data?
var lists: [String: Bool] {
get {
guard let dictionaryData = dictionaryData else {
return [String: Bool]()
}
do {
let dict = try JSONSerialization.jsonObject(with: dictionaryData, options: []) as? [String: Bool]
return dict!
} catch {
return [String: Bool]()
}
}
set {
do {
let data = try JSONSerialization.data(withJSONObject: newValue, options: [])
dictionaryData = data
} catch {
dictionaryData = nil
}
}
}
#objc dynamic var date : Date?
}
class DBrealmToDoList {
var realm: Realm!
func write(_ data: RealmToDoList) throws -> Bool {
var result = false
if (realm != nil) {
try! realm.write {
realm.add(data)
result = true
}
return result
} else {
throw RuntimeError.NoRealmSet
}
}
func getArray() -> Results<RealmToDoList> {
return realm.objects(RealmToDoList.self)
}
func delete(_ data: RealmToDoList) throws -> Bool {
var result = false
if (realm != nil) {
try! self.realm.write {
self.realm.delete(data)
result = true
}
return result
} else {
throw RuntimeError.NoRealmSet
}
}
func update(ofType:Object,value:AnyObject,key:String)->Bool{
do {
let realm = try Realm()
try realm.write {
ofType.setValue(value, forKeyPath: key)
}
return true
}catch let error as NSError {
fatalError(error.localizedDescription)
}
}
func filter(id:Int) -> RealmToDoList? {
let match = realm.objects(RealmToDoList.self).filter("id == %#",id).first
return match
}
func newToDoList(date : Date?,lists: [String: Bool]) -> RealmToDoList{
let pill = RealmToDoList()
pill.date = date
pill.lists = lists
return pill
}
}
I doubt that the matter is in the database, but I cannot understand what it is, because I don’t do a filter by country, etc.
The issue is the date because the date will change based on time zone and if you're selecting today's date/time in one time zone, it will be different that's what's in the database. So if a filter is based on this date
#objc dynamic var date : Date?
then that date will be "today" for whatever time zone you're in but a "today" date that was created this morning in a different time zone will not return the current time zones date.
e.g. if you create a new date/time it will be today in this timezone but could be yesterday in a different time zone.

Issue filtering results from Alamofire by array item in swift4?

So using I am Alamofire and SwiftyJSON I am able to pull data from a JSON source and assign it to a tableview. What I need to do now is filter the results by date. The idea is to grab todays date and only show items that equal or are greater than todays date.
Using Alamofire now when I query the source I get the following
{
"mlb_schedule": {
"columns": [
"awayTeam",
"homeTeam",
"date",
"time"
],
"records": [
[
"TOR",
"BOS",
"7/14/18",
""
],
[
"KCA",
"CHA",
"7/14/18",
""
],
[
"TBA",
"MIN",
"7/14/18",
""
],
[
"MIL",
"PIT",
"7/14/18",
""
],
[
"ARI",
"ATL",
"7/14/18",
""
],
I created a getDate function to pull the current date and send. it to the var dateToday in the code below. Where my issue is exists in Alamofire where I can't seem to select the dict value to filter by the response as shown in the code below.
var arrRes = [Any]()
public var baseballSelectedRow: [String] = []
var dateToday = ""
override func viewDidLoad() {
super.viewDidLoad()
getDate()
//Alamofire
Alamofire.request("http://52.34.250.4/mlb/api.php/mlb_schedule").responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["mlb_schedule"]["records"].arrayObject {
self.arrRes = resData.filter { $0.dict[2] <= dateToday}
}
if self.arrRes.count > 0 {
self.tableView.reloadData()
}
}
}
}
func getDate() {
let currentDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
let convertableDate = dateFormatter.string(from: currentDate)
dateToday = convertableDate
}
}
extension SwarmBaseballViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrRes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "SwarmListTableViewCell") as? SwarmListTableViewCell {
if let dict = arrRes[indexPath.row] as? [Any] {
if dict.count > 2 {
cell.awayLabel?.text = dict[0] as? String
cell.homeLabel?.text = dict[1] as? String
cell.dateLabel?.text = dict[2] as? String
cell.timeLabel?.text = dict[3] as? String
}
}
return cell
} else {
return SwarmListTableViewCell()
}
}
I realize my syntax is wrong but, can't for the life of me find how/why?