Related
I am using Firebase to populate a TableView in my iOS app. The first few objects are loaded but once I get to the third item in my list the app crashes with the exception:
'NSRangeException', reason: '*** __boundsFail: index 3 beyond bounds [0 .. 2]'
I know that this means that I am referring to an array at an index that it does not contain however I do not know why.
I create the TableView with a TableViewController and initialize it like so:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(posts.count)
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
print(post)
let cell = tableView.dequeueReusableCell(withIdentifier: K.cellIdentifier, for: indexPath) as! PostCell
let firstReference = storageRef.child(post.firstImageUrl)
let secondReference = storageRef.child(post.secondImageUrl)
cell.firstTitle.setTitle(post.firstTitle, for: .normal)
cell.secondTitle.setTitle(post.secondTitle, for: .normal)
cell.firstImageView.sd_setImage(with: firstReference)
cell.secondImageView.sd_setImage(with: secondReference)
// Configure the cell...
return cell
}
I believe that the first function creates an array with the number of objects in posts and that the second function assigns values to the template for the cell. The print statement in the first method prints 4 which is the correct number of objects retrieved from firebase. I assume that means an array is created with 4 objects to be displayed in the TableView. This is what is really confusing because the error states that there are only 3 objects in the array. Am I misunderstanding how the TableView is instantiated?
Here is the code that fills the TableView:
func loadMessages(){
db.collectionGroup("userPosts")
.addSnapshotListener { (querySnapshot, error) in
self.posts = []
if let e = error{
print("An error occured trying to get documents. \(e)")
}else{
if let snapshotDocuments = querySnapshot?.documents{
for doc in snapshotDocuments{
let data = doc.data()
if let firstImage = data[K.FStore.firstImageField] as? String,
let firstTitle = data[K.FStore.firstTitleField] as? String,
let secondImage = data[K.FStore.secondImageField] as? String,
let secondTitle = data[K.FStore.secondTitleField] as? String{
let post = Post(firstImageUrl: firstImage, secondImageUrl: secondImage, firstTitle: firstTitle, secondTitle: secondTitle)
self.posts.insert(post, at: 0)
print("Posts: ")
print(self.posts.capacity)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
}
}
The app builds and runs and displays the first few items but crashes once I scroll to the bottom of the list. Any help is greatly appreciated.
Edit:
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.register(UINib(nibName: K.cellNibName, bundle: nil), forCellReuseIdentifier: K.cellIdentifier)
loadMessages()
}
You're getting an out-of-bounds error because you're dangerously populating the datasource. You have to remember that a table view is constantly adding and removing cells as it scrolls which makes updating its datasource a sensitive task. You reload the table on each document iteration and insert a new element in the datasource at index 0. Any scrolling during an update will throw an out-of-bounds error.
Therefore, populate a temporary datasource and hand that off to the actual datasource when it's ready (and then immediately reload the table, leaving no space in between an altered datasource and an active scroll fetching from that datasource).
private var posts = [Post]()
private let q = DispatchQueue(label: "userPosts") // serial queue
private func loadMessages() {
db.collectionGroup("userPosts").addSnapshotListener { [weak self] (snapshot, error) in
self?.q.async { // go into the background (and in serial)
guard let snapshot = snapshot else {
if let error = error {
print(error)
}
return
}
var postsTemp = [Post]() // setup temp collection
for doc in snapshot.documents {
if let firstImage = doc.get(K.FStore.firstImageField) as? String,
let firstTitle = doc.get(K.FStore.firstTitleField) as? String,
let secondImage = doc.get(K.FStore.secondImageField) as? String,
let secondTitle = doc.get(K.FStore.secondTitleField) as? String {
let post = Post(firstImageUrl: firstImage, secondImageUrl: secondImage, firstTitle: firstTitle, secondTitle: secondTitle)
postsTemp.insert(post, at: 0) // populate temp
}
}
DispatchQueue.main.async { // hop back onto the main queue
self?.posts = postsTemp // hand temp off (replace or append)
self?.tableView.reloadData() // reload
}
}
}
}
Beyond this, I would handle this in the background (Firestore returns on the main queue) and only reload the table if the datasource was modified.
After some fiddling around and implementing #bsod's response I was able to get my project running. The solution was in Main.Storyboard under the Attributes inspector I had to set the content to Dynamic Prototypes.
I am using RSSelectionMenu for adding multi-select in my application. It works fine when I add string array. But when I add my model class in it, multiple select feature stops working. It selects all elements when I click on it.
My code:
var filterApiArray: [Model]? = [Model]()
var simpleSelectedArray = [Model]()
func multipleFilterSelection() {
let selectionMenu = RSSelectionMenu(selectionStyle: .multiple, dataSource: filterApiArray ?? []) { (cell, name, indexPath) in
cell.textLabel?.text = name.county
}
selectionMenu.uniquePropertyName = "Model"
selectionMenu.cellSelectionStyle = .checkbox
selectionMenu.show(style: .alert(title: nil, action: AppStrings.done, height: nil), from: self)
selectionMenu.setSelectedItems(items: simpleSelectedArray) { (name, index, selected, selectedItems) in
print(name?.id, index, selected, selectedItems)
}
But if I add a static string array in RSSelectionMenu it works fine. Please comment if anyone works any work around.
This works for me and I am using RS like below:
let data = self.arrAgents.map{$0.agentName ?? ""} as [String]
let menu = RSSelectionMenu(selectionStyle: .multiple, dataSource: data) { (cell, name, indexPath) in
cell.textLabel?.text = name
}
menu.setNavigationBar(title: Strings.AGENT.text)
let selectedData = self.selectedAgent.map{$0.agentName ?? ""} as [String]
menu.setSelectedItems(items: selectedData) { (name, index, selected, selectedItems) in
// self.selectedTags = selectedItems
}
menu.show(from: self)
menu.onRightBarButtonTapped = { selectedItems in
menu.dismiss()
self.selectedAgent.removeAll()
for item in self.arrAgents{
for selectedItem in selectedItems{
if item.agentName == selectedItem{
self.selectedAgent.append(item)
}
}
}
self.collectionView.reloadData()
}
In above code, data is filled with an array named arrAgent and arrAgent has data from API.
I have a table view where depending on the cell class it will download an image from Firebase. I've noticed when using the app that cells with the same cell identifier will show the previous downloaded image before showing the new one. This is what I have before changing it.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableData[indexPath.row]["Image"] != nil {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageNotesData", for: indexPath) as! ImageNotesCell
cell.notes.delegate = self
cell.notes.tag = indexPath.row
cell.notes.text = tableData[indexPath.row]["Notes"] as! String
guard let imageFirebasePath = tableData[indexPath.row]["Image"] else {
return cell }
let pathReference = Storage.storage().reference(withPath: imageFirebasePath as! String)
pathReference.getData(maxSize: 1 * 1614 * 1614) { data, error in
if let error = error {
print(error)
} else {
let image = UIImage(data: data!)
cell.storedImage.image = image
}
}
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "notesData", for: indexPath) as! NotesCell
//let noteString = tableData[indexPath.row]["Notes"] as! String
cell.notes.text = tableData[indexPath.row]["Notes"] as! String
cell.notes.delegate = self
cell.notes.tag = indexPath.row
return cell
}
}
Knowing that this is not a good user experience and that it looks clunky, I tried to move the pathReference.getData to where I setup the data but the view appears before my images finish downloading. I have tried to use a completion handler but I'm still having issues.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
getSectionData(userID: userID, city: selectedCity, completion: {(sectionString) in
self.setupTableCellView(userID: userID, city: selectedCity, section: sectionString) { (tableData) in
DispatchQueue.main.async(execute: {
self.cityName?.text = selectedCity
self.changeSections.setTitle(sectionString, for: .normal)
self.currentSectionString = sectionString
self.setupTableData(tableDataHolder: tableData)
})
}
})
}
func setupTableCellView(userID: String, city: String, section: String, completion: #escaping ([[String:Any]]) -> () ) {
let databaseRef = Database.database().reference().child("Users").child(userID).child("Cities").child(city).child(section)
var indexData = [String:Any]()
var indexDataArray = [[String:Any]]()
databaseRef.observeSingleEvent(of: .value, with: { (snapshot) in
for dataSet in snapshot.children {
let snap = dataSet as! DataSnapshot
//let k = snap.key
let v = snap.value
indexData = [:]
for (key, value) in v as! [String: Any] {
//indexData[key] = value
if key == "Image" {
//let pathReference = Storage.storage().reference(withPath: value as! String)
print("before getImageData call")
self.getImageData(pathRef: value as! String, completion: {(someData) in
print("before assigning indexData[key]")
indexData[key] = someData
print("after assigning indexData[key]")
})
} else {
indexData[key] = value
}
}
indexDataArray.append(indexData)
}
completion(indexDataArray)
})
}
func getImageData(pathRef: String, completion: #escaping(UIImage) -> ()) {
let pathReference = Storage.storage().reference(withPath: pathRef as! String)
pathReference.getData(maxSize: 1 * 1614 * 1614, completion: { (data, error) in
if let error = error {
print(error)
} else {
let image = UIImage(data:data!)
print("called before completion handler w/ image")
completion(image!)
}
})
}
I don't know if I am approaching this the right way but I think I am. I'm also guessing that the getData call is async and that is why it will always download after showing the table view.
You can't do this.
Make the request from Firebase.
Over time, you will get many replies - all the information and all the changing information.
When each new item arrives - and don't forget it may be either an addition or deletion - alter your table so that it displays all the current items.
That's OCC!
OCC is "occasionally connected computing". A similar phrase is "offline first computing". So, whenever you use any major service you use every day like Facebook, Snapchat, etc that is "OCC": everything stays in sync properly whether you do or don't have bandwidth. You know? The current major paradigm of device-cloud computing.
Edit - See Fattie's comments about prepareForReuse()!
With reusable table cells, the cells will at first have the appearance they do by default / on the xib. Once they're "used", they have whatever data they were set to. This can result in some wonky behavior. I discovered an issue where in my "default" case from my data, I didn't do anything ecause it already matched the xib, but if the data's attributes were different, I updated the appearance. The result was that scrolling up and down really fast, some things that should have had the default appearance had the changed appearance.
One basic solution to just not show the previous image would be to show a place holder / empty image, then call your asynchronous fetch of the image. Not exactly what you want because the cell will still show up empty...
Make sure you have a local store for the images, otherwise you're going to be making a server request for images you already have as you scroll up and down!
I'd recommend in your viewDidLoad, call a method to fetch all of your images at once, then, once you have them all, in your success handler, call self.tableview.reloadData() to display it all.
I'm new to RxSwift and trying to learn by creating a simple signup form. I want to implement it using a UITableView (as an exercise, plus it will become more complicated in the future) so I'm currently using two types of cells:
A TextInputTableViewCell with just a UITextField
A ButtonTableViewCell with just a UIButton
In order to represent each cell, I created an enum which looks like that:
enum FormElement {
case textInput(placeholder: String, text: String?)
case button(title: String, enabled: Bool)
}
and use it in a Variable to feed the tableview:
formElementsVariable = Variable<[FormElement]>([
.textInput(placeholder: "username", text: nil),
.textInput(placeholder: "password", text: nil),
.textInput(placeholder: "password, again", text: nil),
.button(title: "create account", enabled: false)
])
by binding like that:
formElementsVariable.asObservable()
.bind(to: tableView.rx.items) {
(tableView: UITableView, index: Int, element: FormElement) in
let indexPath = IndexPath(row: index, section: 0)
switch element {
case .textInput(let placeholder, let defaultText):
let cell = tableView.dequeueReusableCell(withIdentifier: "TextInputTableViewCell", for: indexPath) as! TextInputTableViewCell
cell.textField.placeholder = placeholder
cell.textField.text = defaultText
return cell
case .button(let title, let enabled):
let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonTableViewCell", for: indexPath) as! ButtonTableViewCell
cell.button.setTitle(title, for: .normal)
cell.button.isEnabled = enabled
return cell
}
}.disposed(by: disposeBag)
So far, so good - this is how my form looks like:
Now, the actual problem I'm facing here, is how am I supposed to enable the create account button when all the 3 text inputs are not empty and the password is the same in both password textfields? In other words, what is the right way to apply changes to a cell, based on events happening on one or more of the other cells?
Should my goal be to change this formElementsVariable through the ViewModel or is there any better way to achieve what I want?
I suggest that you change your ViewModel a bit such that you can have more control over the changes in the textfields. If you create streams from your input fields such as username, password and confirmation, you can subscribe for the changes and react to it in any way you want.
Here is how I restructured your code a bit for working with changes in text fields.
internal enum FormElement {
case textInput(placeholder: String, variable: Variable<String>)
case button(title: String)
}
ViewModel.
internal class ViewModel {
let username = Variable("")
let password = Variable("")
let confirmation = Variable("")
lazy var formElementsVariable: Driver<[FormElement]> = {
return Observable<[FormElement]>.of([.textInput(placeholder: "username",
variable: username),
.textInput(placeholder: "password",
variable: password),
.textInput(placeholder: "password, again",
variable: confirmation),
.button(title: "create account")])
.asDriver(onErrorJustReturn: [])
}()
lazy var isFormValid: Driver<Bool> = {
let usernameObservable = username.asObservable()
let passwordObservable = password.asObservable()
let confirmationObservable = confirmation.asObservable()
return Observable.combineLatest(usernameObservable,
passwordObservable,
confirmationObservable) { [unowned self] username, password, confirmation in
return self.validateFields(username: username,
password: password,
confirmation: confirmation)
}.asDriver(onErrorJustReturn: false)
}()
fileprivate func validateFields(username: String,
password: String,
confirmation: String) -> Bool {
guard username.count > 0,
password.count > 0,
password == confirmation else {
return false
}
// do other validations here
return true
}
}
ViewController,
internal class ViewController: UIViewController {
#IBOutlet var tableView: UITableView!
fileprivate var viewModel = ViewModel()
fileprivate let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
viewModel.formElementsVariable.drive(tableView.rx.items) { [unowned self] (tableView: UITableView, index: Int, element: FormElement) in
let indexPath = IndexPath(row: index, section: 0)
switch element {
case .textInput(let placeholder, let variable):
let cell = self.createTextInputCell(at: indexPath,
placeholder: placeholder)
cell.textField.text = variable.value
cell.textField.rx.text.orEmpty
.bind(to: variable)
.disposed(by: cell.disposeBag)
return cell
case .button(let title):
let cell = self.createButtonCell(at: indexPath,
title: title)
self.viewModel.isFormValid.drive(cell.button.rx.isEnabled)
.disposed(by: cell.disposeBag)
return cell
}
}.disposed(by: disposeBag)
}
fileprivate func createTextInputCell(at indexPath:IndexPath,
placeholder: String) -> TextInputTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TextInputTableViewCell",
for: indexPath) as! TextInputTableViewCell
cell.textField.placeholder = placeholder
return cell
}
fileprivate func createButtonCell(at indexPath:IndexPath,
title: String) -> ButtonInputTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonInputTableViewCell",
for: indexPath) as! ButtonInputTableViewCell
cell.button.setTitle(title, for: .normal)
return cell
}
}
We have three different variables based on which we enable disable button, you can see the power of stream and rx operators here.
I think it is always good to convert plain properties to Rx when they change a lot like username, password and passwordField in our case. You can see that formElementsVariable do not change much, it has no real added value of Rx except the magical tableview binding for creating cell.
I think that you are the missing the appropriate rx properties inside the FormElement that will enable you to bind UI events to the validations you want to perform within the ViewModel.
To begin with the FormElement, the textInput should expose a text Variable and the button an enabled Driver. I made this distinction to showcase that in the first case you want to consume UI events while in the second you just want to update the UI.
enum FormElement {
case textInput(placeholder: String, text: Variable<String?>)
case button(title: String, enabled:Driver<Bool>, tapped:PublishRelay<Void>)
}
I took the liberty of adding a tapped event that will enable you to perform your business logic when the button if finally enabled!
Moving on to the ViewModel, I exposed only what the View needs to know but internally I applied all the necessary operators:
class FormViewModel {
// what ViewModel exposes to view
let formElementsVariable: Variable<[FormElement]>
let registerObservable: Observable<Bool>
init() {
// form element variables, the middle step that was missing...
let username = Variable<String?>(nil) // docs says that Variable will deprecated and you should use BehaviorRelay...
let password = Variable<String?>(nil)
let passwordConfirmation = Variable<String?>(nil)
let enabled: Driver<Bool> // no need for Variable as you only need to emit events (could also be an observable)
let tapped = PublishRelay<Void>.init() // No need for Variable as there is no need for a default value
// field validations
let usernameValidObservable = username
.asObservable()
.map { text -> Bool in !(text?.isEmpty ?? true) }
let passwordValidObservable = password
.asObservable()
.map { text -> Bool in text != nil && !text!.isEmpty && text!.count > 5 }
let passwordConfirmationValidObservable = passwordConfirmation
.asObservable()
.map { text -> Bool in text != nil && !text!.isEmpty && text!.count > 5 }
let passwordsMatchObservable = Observable.combineLatest(password.asObservable(), passwordConfirmation.asObservable())
.map({ (password, passwordConfirmation) -> Bool in
password == passwordConfirmation
})
// enable based on validations
enabled = Observable.combineLatest(usernameValidObservable, passwordValidObservable, passwordConfirmationValidObservable, passwordsMatchObservable)
.map({ (usernameValid, passwordValid, passwordConfirmationValid, passwordsMatch) -> Bool in
usernameValid && passwordValid && passwordConfirmationValid && passwordsMatch // return true if all validations are true
})
.asDriver(onErrorJustReturn: false)
// now that everything is in place, generate the form elements providing the ViewModel variables
formElementsVariable = Variable<[FormElement]>([
.textInput(placeholder: "username", text: username),
.textInput(placeholder: "password", text: password),
.textInput(placeholder: "password, again", text: passwordConfirmation),
.button(title: "create account", enabled: enabled, tapped: tapped)
])
// somehow you need to subscribe to register to handle for button clicks...
// I think it's better to do it from ViewController because of the disposeBag and because you probably want to show a loading or something
registerObservable = tapped
.asObservable()
.flatMap({ value -> Observable<Bool> in
// Business login here!!!
NSLog("Create account!!")
return Observable.just(true)
})
}
}
Finally, on your View:
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
private let disposeBag = DisposeBag()
var formViewModel: FormViewModel = FormViewModel()
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "TextInputTableViewCell", bundle: nil), forCellReuseIdentifier: "TextInputTableViewCell")
tableView.register(UINib(nibName: "ButtonTableViewCell", bundle: nil), forCellReuseIdentifier: "ButtonTableViewCell")
// view subscribes to ViewModel observables...
formViewModel.registerObservable.subscribe().disposed(by: disposeBag)
formViewModel.formElementsVariable.asObservable()
.bind(to: tableView.rx.items) {
(tableView: UITableView, index: Int, element: FormElement) in
let indexPath = IndexPath(row: index, section: 0)
switch element {
case .textInput(let placeholder, let defaultText):
let cell = tableView.dequeueReusableCell(withIdentifier: "TextInputTableViewCell", for: indexPath) as! TextInputTableViewCell
cell.textField.placeholder = placeholder
cell.textField.text = defaultText.value
// listen to text changes and pass them to viewmodel variable
cell.textField.rx.text.asObservable().bind(to: defaultText).disposed(by: self.disposeBag)
return cell
case .button(let title, let enabled, let tapped):
let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonTableViewCell", for: indexPath) as! ButtonTableViewCell
cell.button.setTitle(title, for: .normal)
// listen to viewmodel variable changes and pass them to button
enabled.drive(cell.button.rx.isEnabled).disposed(by: self.disposeBag)
// listen to button clicks and pass them to the viewmodel
cell.button.rx.tap.asObservable().bind(to: tapped).disposed(by: self.disposeBag)
return cell
}
}.disposed(by: disposeBag)
}
}
}
Hope I helped!
PS. I am mainly an Android developer but I found your question (and bounty) intriguing so please forgive any rough edges with (rx)swift
You would do better to emit table data all at once rather than one row at a time because otherwise you can't really distinguish between a) is this next event a new row or b) is this next event a refresh of a row I already showed.
Given that here's one way to do it. This would go in the ViewModel and present the table data as an observable. You can then bind the text fields for the username/password to the properties (behavior relays) though probably nicer to not expose them as such to the UI (hide behind properties)
var userName = BehaviorRelay<String>(value: "")
var password1 = BehaviorRelay<String>(value: "")
var password2 = BehaviorRelay<String>(value: "")
struct LoginTableValues {
let username: String
let password1: String
let password2: String
let createEnabled: Bool
}
func tableData() -> Observable<LoginTableValues> {
let createEnabled = Observable.combineLatest(userName.asObservable(), password1.asObservable(), password2.asObservable())
.map { (username: String, password1: String, password2: String) -> Bool in
return !username.isEmpty &&
!password1.isEmpty &&
password1 == password2
}
return Observable.combineLatest(userName.asObservable(), password1.asObservable(), password2.asObservable(), createEnabled)
.map { (arg: (String, String, String, Bool)) -> LoginTableValues in
let (username, password1, password2, createEnabled) = arg
return LoginTableValues(username: username, password1: password1, password2: password2, createEnabled: createEnabled)
}
}
Firstly, you may want to try RxDataSources which is an RxSwift wrapper for TableViews. Secondly, to answer your question, I would have done the change through the ViewModel- that is, provide a ViewModel for the cell and then in the ViewModel set an observable that will handle the validation. When all of that is setup, do a combineLatest on all the cell's validation observables.
I need your help! I don´t know how to change an array that is inserted on a TableCell from information I have in another ViewController. It’s a little bit messed up, but I’m gonna show you by my code.
Here I have a ViewController conformed by many switches that correspond to different categories of coupons, this is the code:
class FiltersViewController: UIViewController {
#IBOutlet weak var restaurantsSwitch: UISwitch!
#IBOutlet weak var sportsSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func returnHome(_ sender: Any) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "home") as! HomeViewController
self.present(vc, animated: false, completion: nil)
}
#IBAction func restaurants(_ sender: UISwitch) {
if restaurantsSwitch.isOn == true{
tuxtlaSwitch.isOn = false
sevillaSwitch.isOn = false
coapaSwitch.isOn = false
coyoacanSwitch.isOn = false
universidadSwitch.isOn = false
polancoSwitch.isOn = false
}
}
#IBAction func sports(_ sender: UISwitch) {
if sportsSwitch.isOn == true{
tuxtlaSwitch.isOn = false
sevillaSwitch.isOn = false
coapaSwitch.isOn = false
coyoacanSwitch.isOn = false
universidadSwitch.isOn = false
polancoSwitch.isOn = false
}
}
}
I’ve only show you two switches at the example with the purpose of not filling this with many code, but there are like 15 switches.
And in the other ViewController, which is connected to this one, the HomeViewController, contains coupons that comes from a JSON, and conforms an array of ten items displayed on a TableViewCell, the code:
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var data : NSArray = []
var mainData : NSArray = []
var couponsImg : [UIImage] = []
var couponsTitle : [String] = []
var couponsDesc : [String] = []
var couponsCat : [String] = []
func getCoupons(){
let miURL = URL(string: RequestConstants.requestUrlBase)
let request = NSMutableURLRequest(url: miURL!)
request.httpMethod = "GET"
if let data = try? Data(contentsOf: miURL! as URL) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary
let parseJSON = json
let object = parseJSON?["object"] as! NSDictionary
let mainCoupon = object["mainCoupon"] as! NSArray
let coupons = object["coupons"] as! NSArray
self.mainData = mainCoupon
self.data = coupons
self.couponImg1 = (mainCoupon[0] as AnyObject).value(forKey: "urlImage") as! String
self.couponImg2 = (mainCoupon[1] as AnyObject).value(forKey: "urlImage") as! String
self.couponTitle1 = (mainCoupon[0] as AnyObject).value(forKey: "nameStore") as! String
self.couponTitle2 = (mainCoupon[1] as AnyObject).value(forKey: "nameStore") as! String
self.couponDesc1 = (mainCoupon[0] as AnyObject).value(forKey: "promoDescription") as! String
self.couponDesc2 = (mainCoupon[1] as AnyObject).value(forKey: "promoDescription") as! String
self.couponCat1 = (mainCoupon[0] as AnyObject).value(forKey: "category") as! String
self.couponCat2 = (mainCoupon[1] as AnyObject).value(forKey: "category") as! String
self.couponsImg = [couponImage1!, couponImage2!, couponImage3!, couponImage4!, couponImage5!, couponImage6!, couponImage7!, couponImage8!, couponImage9!, couponImage10!]
self.couponsTitle = [couponTitle1, couponTitle2, couponTitle3, couponTitle4, couponTitle5, couponTitle6, couponTitle7, couponTitle8, couponTitle9, couponTitle10]
self.couponsDesc = [couponDesc1, couponDesc2, couponDesc3, couponDesc4, couponDesc5, couponDesc6, couponDesc7, couponDesc8, couponDesc9, couponDesc10]
self.couponsCat = [couponCat1, couponCat2, couponCat3, couponCat4, couponCat5, couponCat6, couponCat7, couponCat8, couponCat9, couponCat10]
} catch {
let error = ErrorModel()
error.phrase = "PARSER_ERROR"
error.code = -1
error.desc = "Parser error in get Notifications action"
}
}
}
#IBAction func showFilters(_ sender: Any) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "filters") as! FiltersViewController
self.present(vc, animated: false, completion: nil)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableViewCell
cell.couponImg.image = couponsImg[indexPath.row]
cell.couponTitle.text = couponsTitle[indexPath.row]
cell.couponDescription.text = couponsDesc[indexPath.row]
cell.couponCategory.text = couponsCat[indexPath.row]
return cell
}
(Again I’ve only showed you two coupons for the example). The thing is that I need to apply some filters to the coupons on the TableCell. The first time the view appear it shows the 10 coupons correctly, but when I go to the filters an put it some of them ON it doesn’t make a difference, the method I was trying to use was something like this, first have an instance of the FiltersViewController class:
var filters = FilterViewController()
if filters.isMovingToParentViewController == true {
if filters.restaurantsSwitch.isOn == false {
self.couponsImg.remove(at: 0)
self.couponsImg.remove(at: 1)
self.couponsImg.remove(at: 2)
}
if filters.sportsSwitch.isOn == false {
self.couponsImg.remove(at: 3)
self.couponsImg.remove(at: 4)
self.couponsImg.remove(at: 5)
}
}
In the example bellow I’m trying to say that if a have the restaurant switch off, I’m going to delete the corresponding coupons of the restaurant category, and the same with the sports switch. But first of all I don’t know where to include this logic, in which method? And also I don’t know if this instruction is correct for my purposes. Can somebody give me a hand please???
Your logic is not working because you're instantiating a new FilterViewController, different from the FilterViewController associated with you screen.
You can solve this using delegate.
First, create the delegate:
protocol FilterDelegate {
func updateTable() }
Then, In your FilterViewController add this line:
weak var delegate:FilterDelegate?
You HomeViewController have to conform with this delegate, so:
class HomeViewController: FilterDelegate ... {
func updateTable() {
/* GET THE DATA FILTERED HERE */
tableview.reloadData()
}
In your FilterViewController:
#IBAction func returnHome(_ sender: Any) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "home") as! HomeViewController
self.delegate = vc
self.present(vc, animated: false, completion: nil)
delegate?.updateTable()
}
I think that should work.
EDIT:
Another approach is to create a segue between these two vcs and pass the which filters are active using the "prepare" function . Then you can take this information in your HomeVC and load your table based on the filters in the viewDidLoad function.
1 - Create a object Filters:
class Filters {
var tuxtlaSwitchIsOn: Bool
var sevillaSwitchIsOn: Bool
...
init(tuxtlaSwitchIsOn: Bool, sevillaSwitchIsOn: Bool, ...) {
self.tuxtlaSwitchIsOn = tuxtlaSwitchIsOn
self.sevillaSwitchIsOn = sevillaSwitchIsOn
...
}
}
2 - Add a attribute Filters to your HomeVC
class HomeViewController : ... {
...
var filtersActive: Filters?
...
}
3 - In your FilterViewController instantiate a Filter object indicating which filters are on
4 - In your FilterViewController prepare funs pass the Filter object to HomeVC
5 - In your HomeVC, get the Filter object and filter your data based on it.
Sure here is what you need. So you have a set of array filled with data and you want to apply filter on them. First, you need to create another array for filter results. This is because when user removes the filter, you still want to show the full list. To simplify, say you only have an array Foo: [String]. So you need to create another array called FooFiltered: [String] to hold the search result. Your can leave it empty when the view controller is loaded.
Next, in your filter section, it's recommended to use array filter technology like this post, but it's okay if you want to do it in your way. So all you need to do is to get elements from Foo array that match certain criteria and copy them into FooFiltered array. Here let me show you an example of doing filter manually
func filter() {
FooFiltered = [String]() //Clean up every time before search
for str in Foo {
if str == "criteria" {
FooFiltered.append(str)
}
}
}
Now you have a list of filtered items. You need a flag to tell table view which set of array to display. Say you have a flag called showSearchResult that is set to false originally. When you do the filter, set it to true. So your cellForRow will look like
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if showSearchResult {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
cell.textField.text = FooFiltered[indexPath.row]
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
cell.textField.text = Foo[indexPath.row]
return cell
}
}
You also need to update this flag to all your table view delegate method, like numberOfRowsInSection, etc.
Finally, with these codes, your table view is configured to show full results or filtered results base on the flag and you are setting that flag in the filter() function. The last thing to do is to ask tableView to reload data when the filter is done. So modify your filter function like this and you should be all set.
func filter() {
FooFiltered = [String]() //Clean up every time before search
showSearchResul = true
for str in Foo {
if str == "criteria" {
FooFiltered.append(str)
}
}
self.tableView.reloadData()
}