So im populating a table View from firebase database. I am able to add and remove check marks. But i can't seem to figure out how to save it. Since the tableView reloads the data every time the view appears.
here is my view controller
import UIKit
import FirebaseDatabase
import Firebase
class guestListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var guestListTableView: UITableView!
var guestListDBRef : DatabaseReference!
var guestListText = [AdminTextModel]()
var keyArray : [String] = []
override func viewDidLoad() {
super.viewDidLoad()
guestListDBRef = Database.database().reference().child("RSVP")
guestListDBRef.queryOrdered(byChild: "name").observe(DataEventType.value, with: {(snapshot) in
if snapshot.childrenCount > 0 {
for guestListLabel in snapshot.children.allObjects as! [DataSnapshot] {
let guestListTextObject = guestListLabel.value as? [String: AnyObject]
let name = guestListTextObject?["name"]
let date = guestListTextObject?["date"]
let guestListTextLabels = AdminTextModel(name: name as! String?, date: date as! String?)
self.guestListText.append(guestListTextLabels)
self.guestListTableView.rowHeight = 45
self.guestListTableView.reloadData()
self.getKeys()
}
}
})
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return guestListText.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let guestListTextCell = tableView.dequeueReusableCell(withIdentifier: "guestList") as! guestListTableViewCell
let text: AdminTextModel
text = guestListText[indexPath.row]
guestListTextCell.guestListNameLabel.text = text.name
guestListTextCell.guestListDateLabel.text = text.date
return guestListTextCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark {
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
} else {
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
My AdminTextModel
import Foundation
class AdminTextModel {
var name: String?
var date: String?
init(name: String?, date: String?) {
self.name = name
self.date = date
}
}
And my TableViewCell
import UIKit
class guestListTableViewCell: UITableViewCell {
#IBOutlet weak var guestListDateLabel: UILabel!
#IBOutlet weak var guestListNameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Let me know if you have any input!
enter image description here
enter image description here
enter image description hereng
Change your model to this
class AdminTextModel {
var name: String?
var date: String?
var isChecked: Bool?
init(name: String?, date: String?, isChecked: Bool?) {
self.name = name
self.date = date
self.isChecked = isChecked
}
}
The isChecked property should also be saved in your firebase database
Now fetch the data and set the checkmark to true or false based on the isChecked property.
This will retain the checkmark even after reloading the screen
Thank you!, I went ahead and implemented that iParesh, but i think my issue lies here. Since im using childByAutoID.
let onlinesRef = Database.database().reference().child("RSVP").child("yourchild name")
As per Moaz Khan change model class and also need to change in cellForRowAt with below code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let guestListTextCell = tableView.dequeueReusableCell(withIdentifier: "guestList") as! guestListTableViewCell
let text: AdminTextModel
text = guestListText[indexPath.row]
guestListTextCell.guestListNameLabel.text = text.name
guestListTextCell.guestListDateLabel.text = text.date
if text.isChecked {
cell.accessoryType = .checkmark
}else {
cell.accessoryType = .none
}
return guestListTextCell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let isChecked = !self.guestListText[indexPath.row].isChecked
let key = self.guestListText[indexPath.row].key
Checkedservice.checkuncheck(key: key, isChecked: isChecked) { (seccess) in
guard seccess else { return }
self.guestListText[indexPath.section].isChecked = isChecked
self.tableView.reloadRows(at: [indexPath], with: .none)
}
}
Check mark service for firebase
struct Checkedservice {
static func checkuncheck(key: String, isChecked: Bool, success: #escaping (Bool) -> Void) {
let onlinesRef = Database.database().reference().child("RSVP").child(key).child("isChecked")
onlinesRef.setValue(isChecked) {(error, _ ) in
if let error = error {
assertionFailure(error.localizedDescription)
success(false)
}
success(true)
}
}
}
Update model class
class AdminTextModel {
var key: String?
var name: String?
var date: String?
var isChecked: Bool?
init(key: String?, name: String?, date: String?, isChecked: Bool?) {
self.key = key
self.name = name
self.date = date
self.isChecked = isChecked
}
}
Related
I have a tableview that has a list of users in a directory that are loaded in from a firebase directory. My question is how can i put them into sections similarly to that of the contact app in the iPhone. I want each user to be displayed by their name under the appropriate sections.
here is the code I have:
my main view controller:
import UIKit
import Foundation
import Firebase
import FirebaseDatabase
class ViewController: UIViewController {
//MARK: - variables for the textfield search
#IBOutlet var field: UITextField!
var filteredTable = [String]()
//MARK: - Variables for the tableview data from Firebase
var table = [FacStaffInfo]()
var ref: DatabaseReference!
#IBOutlet weak var Tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//MARK: - setting the delegate for the test field for filter
field.delegate = self
//MARK: - This is all the functionality for the loading from Firebase to the TableView
ref = Database.database().reference().child("users")
ref.observe(DataEventType.value, with: {(snapshot) in
if snapshot.childrenCount > 0 {
self.table.removeAll()
for user in snapshot.children.allObjects as! [DataSnapshot] {
let object = user.value as? [String: AnyObject]
let title = object?["title"]
let name = object?["name"]
let email = object?["email"]
let phone = object?["phone"]
let office = object?["office"]
let bio = object?["bio"]
let user = FacStaffInfo(title: title as! String, name: name as! String, email: email as! String, phone: phone as! Int, office: office as! String, bio: bio as! String)
self.table.append(user)
self.Tableview.reloadData()
}
}
})
}
}
//MARK: - add the tableview functions
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//MARK: - adding functionality for textfield search
if !filteredTable.isEmpty {
return table.count
}
//MARK: - just returning the array from the database, keep if remove textfield crap
return table.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "userCell") as! TableViewCell
let user: FacStaffInfo
user = table[indexPath.row]
cell.titleLabel?.text = user.title
cell.nameLabel?.text = user.name
cell.emailLabel?.text = user.email
cell.phoneLabel?.text = String(user.phone)
cell.officeLabel?.text = user.office
cell.bioLabel?.text = user.bio
//MARK: - testing using arrays to filter tableview data
// if !filteredTable.isEmpty {
// cell.textLabel?.text = filteredTable[indexPath.row]
// }
// else {
// cell.textLabel?.text = table.
// }
//
//
//
//
//
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = Tableview.indexPathForSelectedRow {
let destinationController = segue.destination as! InfoViewController
destinationController.FacStaffData = table[indexPath.row]
}
}
}
}
This is my data model
import Foundation
import Firebase
import FirebaseDatabase
class FacStaffInfo {
var title: String
var name: String
var email: String
var phone: Int
var office: String
var bio: String
init(title: String, name: String, email: String, phone: Int, office: String, bio: String) {
self.title = title;
self.name = name;
self.email = email;
self.phone = phone;
self.office = office;
self.bio = bio
}
}
This is my table view cell
import UIKit
import Firebase
import FirebaseDatabase
class TableViewCell: UITableViewCell {
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var emailLabel: UILabel!
#IBOutlet weak var phoneLabel: UILabel!
#IBOutlet weak var officeLabel: UILabel!
#IBOutlet weak var bioLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Swift 5
Declare your class as follows:
class ViewController: UIViewController
{
// MARK: Outlets
#IBOutlet weak var Tableview: UITableView!
#IBOutlet weak var field: UITextField!
// MARK: Properties
var sectionNames: [String] = []
var users: [String: [FacStaffInfo]] = [:]
var ref: DatabaseReference!
// MARK: View Controller Life Cycle
override func viewDidLoad()
{
super.viewDidLoad()
getUsersFromFirebaseDB()
}
deinit
{
ref.removeAllObservers()
}
// MARK: Private Methods
private func usersFetched(_ usersData: [FacStaffInfo])
{
for user in usersData
{
guard let userNameFirstChar = user.name.first?.uppercased() else { continue }
if var usersForKey = users["\(userNameFirstChar)"]
{
usersForKey.append(user)
users["\(userNameFirstChar)"] = usersForKey
}
else
{
// no users are stored in dictionary for key userNameFirstChar
users["\(userNameFirstChar)"] = [user]
}
}
// sort dictionary keys and set it in sectionNames
sectionNames = users.map { $0.key }.sorted()
}
private func getUsersFromFirebaseDB()
{
ref = Database.database().reference().child("users")
ref.observe(DataEventType.value, with: { [weak self] (snapshot) in
guard snapshot.childrenCount > 0 else { return }
var users: [FacStaffInfo] = []
for user in snapshot.children.allObjects as! [DataSnapshot]
{
let object = user.value as? [String: AnyObject]
let title = object?["title"]
let name = object?["name"]
let email = object?["email"]
let phone = object?["phone"]
let office = object?["office"]
let bio = object?["bio"]
let user = FacStaffInfo(title: title as! String, name: name as! String, email: email as! String, phone: phone as! Int, office: office as! String, bio: bio as! String)
users.append(user)
}
self?.usersFetched(users)
self?.Tableview.reloadData()
})
}
// MARK: Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "showDetail"
{
if let indexPath = Tableview.indexPathForSelectedRow
{
let destinationController = segue.destination as! InfoViewController
let char = sectionNames[indexPath.section]
let user = users[char]![indexPath.row]
destinationController.FacStaffData = user
}
}
}
}
Also, add the following extension:
extension ViewController: UITableViewDataSource, UITableViewDelegate
{
func numberOfSections(in tableView: UITableView) -> Int
{
sectionNames.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
sectionNames[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
let char = sectionNames[section]
return users[char]!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "userCell") as! TableViewCell
let char = sectionNames[indexPath.section]
let user = users[char]![indexPath.row]
cell.nameLabel?.text = user.name
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
performSegue(withIdentifier: "showDetail", sender: self)
tableView.deselectRow(at: indexPath, animated: true)
}
}
Output
I am trying to create a one to many relationship, otherwise known as a parent child relationship, in realm. I looked at the documentation that realm offers but I am still a little stuck on how to do the actual saving to realm. I have two views, the main view is a view controller that just has a tableview with the numbers 1-7. In this view i can mass select for editing these rows in the table and save them to realm. That part is no big deal.
On the next view I have something very similar where there is a tableview with just some sample data. There is a mass select rows button, that is fine, it is the save button that I am having trouble with. The data in this tableView, which is the same on all of them just for testing purposes, is data i want to have a child relationship with the data on the first view.
For example if I have 4 saved to realm I click the row with 4 on it and it takes me to my next View. The tableview on this has two rows and other data, but i want to be able to mass select these rows and save them as a child to 4. I am a little confused as to what the save to realm function would look like.
this is my first view controller
import UIKit
import Realm
import RealmSwift
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var realm: Realm!
fileprivate var createSaves = SaveClass.createSaves()
var testingBool = false
var values: [String] = []
var valuesTwo: [String] = []
var valuesThree: [String] = []
#IBOutlet weak var itemBtn: UIBarButtonItem!
#IBOutlet weak var saveBtn: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
print(Realm.Configuration.defaultConfiguration.fileURL!)
realm = try! Realm()
self.tableView.delegate = self
self.tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return createSaves.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.txtLbl?.text = "\(createSaves[indexPath.row].label)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if testingBool == true {
values.append(createSaves[indexPath.row].label)
valuesTwo.append(createSaves[indexPath.row].romanNum)
valuesThree.append(createSaves[indexPath.row].txt)
} else if testingBool == false {
performSegue(withIdentifier: "segue", sender: indexPath)
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if testingBool == true {
if let index = values.index(of: createSaves[indexPath.row].label) {
values.remove(at: index)
}
if let index = valuesTwo.index(of: createSaves[indexPath.row].romanNum) {
valuesTwo.remove(at: index)
}
if let index = valuesThree.index(of: createSaves[indexPath.row].txt) {
valuesThree.remove(at: index)
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let nxtVC = segue.destination as? TestingViewController
let myIndexPath = self.tableView.indexPathForSelectedRow!
let row = myIndexPath.row
nxtVC?.realmedData = createSaves[row].label
}
#IBAction func btnPressed(_ sender: Any) {
testingBool = !testingBool
if testingBool == true {
tableView.allowsMultipleSelection = true
tableView.allowsMultipleSelectionDuringEditing = true
itemBtn.title = "cancel"
} else if testingBool == false {
tableView.allowsMultipleSelection = false
tableView.allowsMultipleSelectionDuringEditing = false
itemBtn.title = "item"
}
}
#IBAction func saveBtnPressed(_ sender: Any) {
if testingBool == true {
//favorite(label: values)
realmed(label: values, romanNum: valuesTwo, txt: valuesThree)
}
}
func realmed(label: [String], romanNum: [String], txt: [String]) {
try? realm!.write {
for (stringOne, (stringTwo, stringThree)) in zip(label, zip(romanNum, txt)) {
let realmed = Realmed(label: stringOne, romanNum: stringTwo, txt: stringThree)
realm.add(realmed)
}
}
}
}
this is my second view
import UIKit
import Realm
import RealmSwift
class TestingViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var mainLbl: UILabel!
var realm: Realm!
var realmedData = ""
var testingBool = false
var values: [String] = []
var valuesTwo: [String] = []
#IBOutlet weak var testTable: UITableView!
#IBOutlet weak var selectBtn: UIButton!
#IBOutlet weak var saveBtn: UIButton!
let firstSave = OtherSave.otherArrOne()
override func viewDidLoad() {
super.viewDidLoad()
realm = try! Realm()
self.testTable.delegate = self
self.testTable.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if objectExists(label: realmedData) == true {
self.mainLbl.text = "\(realmedData)"
} else {
self.mainLbl.text = "Don't Know"
}
}
#IBAction func selectBtnPressed(_ sender: Any) {
testingBool = !testingBool
if testingBool == true {
testTable.allowsMultipleSelection = true
testTable.allowsMultipleSelectionDuringEditing = true
} else if testingBool == false {
testTable.allowsMultipleSelection = false
testTable.allowsMultipleSelectionDuringEditing = false
}
}
#IBAction func saveBtnPressed(_ sender: Any) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return firstSave.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! TestingTableViewCell
cell.nameLbl.text = "\(firstSave[indexPath.row].name)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if testingBool == true {
values.append(firstSave[indexPath.row].info)
valuesTwo.append(firstSave[indexPath.row].name)
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if testingBool == true {
if let index = values.index(of: firstSave[indexPath.row].info) {
values.remove(at: index)
}
if let index = valuesTwo.index(of: firstSave[indexPath.row].name) {
valuesTwo.remove(at: index)
}
}
}
func fetchingLabel(label: String) -> Realmed? {
let realm = try? Realm()
return realm?.object(ofType: Realmed.self, forPrimaryKey: label)
}
func objectExists(label: String) -> Bool {
return realm.object(ofType: Realmed.self, forPrimaryKey: label) != nil
}
}
this is the parent's realm object:
import Foundation
import Realm
import RealmSwift
class Realmed: Object {
#objc dynamic var label = ""
#objc dynamic var romanNum = ""
#objc dynamic var txt = ""
let realmTwo = List<RealmTwo>()
override static func primaryKey() -> String {
return "label"
}
convenience init(label: String, romanNum: String, txt: String) {
self.init()
self.label = label
self.romanNum = romanNum
self.txt = txt
}
}
this is the child's realm object
import Foundation
import UIKit
import Realm
import RealmSwift
class RealmTwo: Object {
#objc dynamic var spanish = String()
#objc dynamic var french = String()
let realmed = LinkingObjects(fromType: Realmed.self, property: "realmTwo")
}
I have tried to do a function similar to the first view controllers:
func realmed(label: [String], romanNum: [String], txt: [String]) {
try? realm!.write {
for (stringOne, (stringTwo, stringThree)) in zip(label, zip(romanNum, txt)) {
let realmed = Realmed(label: stringOne, romanNum: stringTwo, txt: stringThree)
realm.add(realmed)
}
}
}
but I don't really understand how to create an instance of the data that i want to save as an array.
If there is anything i can help with, please ask
Thank you
I assume you want to create “RealmTwo” objects from “values” and “valuesTwo” and add them to a Realmed object .
What you want to do is
Fetch a parent object (Realmed)
Create a child object (RealmTwo)
Append the child to the parent object
You can do that with a function like this.
func save() {
let realmed = fetchingLabel(label: realmedData)! // fetch a parent
do {
try realm.write {
for index in 0..<values.count {
let realmTwo = RealmTwo() // create a child
realmTwo.french = values[index]
realmTwo.spanish = valuesTwo[index]
realmed.realmTwo.append(realmTwo) // append
}
}
} catch {
}
}
I have a journal application that has an object called Entry. It has its own Swift file called Entry.swift and these journal entries are saved using arrays of dictionaries.
I added a search bar to the UITableViewController and whenever I input a letter the app crashes after tableView.reloadData() is called. I think this has something to do with the filter returning my array of dictionaries named entries incorrectly and when tableView.reloadData() is called, both of the labels on the dequeueReusableCell can't be populated because the information is in the wrong format in the array of dictionaries.
Entry.swift
//
// Entry.swift
// Journal
//
// Created by handje on 6/17/17.
// Copyright © 2017 Rob Hand. All rights reserved.
//
import Foundation
class Entry {
static fileprivate let titleKey = "title"
static fileprivate let bodyTextKey = "bodyText"
static fileprivate let dateKey = "date"
var title: String
var bodyText: String
var date: String
init(title: String, bodyText: String, date: String ) {
self.title = title
self.bodyText = bodyText
self.date = date
}
func dictionaryRepresentation() -> [String: Any] {
return [Entry.titleKey: title, Entry.bodyTextKey: bodyText, Entry.dateKey: date]
}
convenience init?(dictionary: [String: Any]) {
guard let title = dictionary[Entry.titleKey] as? String,
let bodyText = dictionary[Entry.bodyTextKey] as? String, let date = dictionary[Entry.dateKey] as? String else { return nil
}
self.init(title: title, bodyText: bodyText, date: date)
}
}
extension Entry: Equatable {
static func == (lhs:Entry, rhs:Entry) -> Bool {
return
lhs.title == rhs.title &&
lhs.bodyText == rhs.bodyText
}
}
EntryListTableViewController.swift
//
// EntryListTableViewController.swift
// Journal
//
// Created by handje on 6/17/17.
// Copyright © 2017 Rob Hand. All rights reserved.
//
import UIKit
class EntryListTableViewCell: UITableViewCell {
#IBOutlet weak var dreamTitle: UILabel!
#IBOutlet weak var dreamDate: UILabel!
}
extension EntryListTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchText: searchController.searchBar.text!)
}
}
class EntryListTableViewController: UITableViewController {
#IBOutlet weak var searchBar: UISearchBar!
var dreamTitle: UILabel!
let searchController = UISearchController(searchResultsController: nil)
let dreams = EntryController.shared.entries
var filteredDreams = [Entry]()
func filterContentForSearchText(searchText: String, scope: String = "All") {
let filteredDreams = EntryController.shared.entries.filter{ $0.title.contains(searchController.searchBar.text!) }
tableView.reloadData()
print(filteredDreams)
}
override func viewDidLoad() {
//cell setup
super.viewDidLoad()
let backgroundImage = UIImage(named: "DreamPageLucidity.jpg")
let imageView = UIImageView(image: backgroundImage)
imageView.contentMode = .scaleAspectFill
self.tableView.backgroundView = imageView
tableView.separatorInset = .zero
tableView.separatorColor = UIColor.lightGray
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return EntryController.shared.entries.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "entryCell", for: indexPath) as! EntryListTableViewCell
let entry: Entry
if searchController.isActive && searchController.searchBar.text != "" {
entry = filteredDreams[indexPath.row] /////////ERROR HERE///////
} else {
entry = EntryController.shared.entries[indexPath.row]
}
cell.dreamTitle.text = entry.title
cell.dreamDate.text = entry.date
if cell.dreamTitle.text == "" {
cell.dreamTitle.text = "Untitled Dream"
}
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let entry = EntryController.shared.entries[indexPath.row]
EntryController.shared.deleteEntry(entry: entry)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailVC = segue.destination as? EntryDetailViewController
guard let indexPath = tableView.indexPathForSelectedRow else { return }
let entry = EntryController.shared.entries[indexPath.row]
detailVC?.entry = entry
}
}
EntryContoller.swift
//
// EntryController.swift
// Journal
//
// Created by handje on 6/17/17.
// Copyright © 2017 Rob Hand. All rights reserved.
//
import Foundation
class EntryController {
var entries = [Entry]()
static fileprivate let entriesKey = "entriesKey"
static let shared = EntryController()
init() {
load()
}
// MARK: - CRUD
func addNewEntryWith(title: String, bodyText: String, date: String) {
let entry = Entry(title: title, bodyText: bodyText, date: date)
entries.append(entry)
save()
}
func updateEntry(entry: Entry, title: String, bodyText: String, date: String) {
entry.title = title
entry.bodyText = bodyText
save()
}
// Set up search bar
func deleteEntry(entry: Entry) {
guard let index = entries.index(of: entry) else { return }
entries.remove(at: index)
save()
}
// MARK: - save/load UserDefaults
private func save() {
let entryDictionaries = entries.map {$0.dictionaryRepresentation()}
UserDefaults.standard.set(entryDictionaries, forKey: EntryController.entriesKey)
}
private func load() {
guard let entryDictionaries = UserDefaults.standard.object(forKey: EntryController.entriesKey) as? [[String: Any]] else { return }
entries = entryDictionaries.flatMap ({ Entry(dictionary: $0) })
}
}
I think there is a problem with
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return EntryController.shared.entries.count
}
You should put a check here that if search controller is active then return count from filteredDreams else return count from EntryController.shared.entries.count, (make code changes as per your exact implementation) something like:
if searchController.isActive && searchController.searchBar.text != "" {
return filterDreams.count
} else {
return EntryController.shared.entries.count
}
You're returning a list that is bigger than the filtered list in the delegate function numberOfRowsInSection
try this, when searching:
func filterContentForSearchText(searchText: String, scope: String = "All") {
// update the list that is a class property, you were creating a new one
if searchText.isEmpty {
filteredDreams = EntryController.shared.entries
} else {
filteredDreams = EntryController.shared.entries.filter{ $0.title.contains(searchController.searchBar.text!) }
}
tableView.reloadData()
}
in numberOfRowsInSection
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// use the filtered list to determine count
return filteredDreams.count
}
for more safety you can return an empty cell instead of crashing:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard indexPath.row < filteredDreams.count else { return UITableViewCell() }
// your code here
}
I have 2 section with each 16 rows,and how to get all textfield value in tableview cell? want to store it when I tap save button.
and I already retrive simulate data from firebase database put in String:AnyObject,and show it on tableview.
how to get value like textfield or switch in tableviewCell?
import UIKit
import Firebase
import FirebaseDatabaseUI
class SettingLabelTableViewController: UITableViewController, UITextFieldDelegate {
var BitArray:[String] = ["M0","M1","M2","M3","M4","M5","M6","M0","M8"
,"M9","M10","M11","M12","M13","M14","M15"]
var WordArray:[String] = ["D0","D1","D2","D3","D4","D5","D6","D0","D8"
,"D9","D10","D11","D12","D13","D14","D15"]
var DeviceKey:String?
var Ref: FIRDatabaseReference!
var dict = [String:AnyObject]()
var allCellsText = [String]()
#IBAction func SaveButton(_ sender: UIBarButtonItem)
{
/*self.Ref.setValue(dict, withCompletionBlock:
{ (error, dbref) in
})*/
}
override func viewDidLoad()
{
super.viewDidLoad()
Ref = device.child("DEVICE").child(DeviceKey!).child("SETTINGS").child("LABEL")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
Ref.observeSingleEvent(of: .value, with: { (snapshot) in
if !snapshot.exists(){
print("not exist")
csGolbal.g_NameAry.removeAll()
self.dict.removeAll()
}
else{
self.dict.removeAll()
self.dict = (snapshot.value as? [String: AnyObject])!
/*for item in snapshot.value as! [String : String]{
csGolbal.g_NameAry.append([item.key:item.value])
}*/
}
self.tableView.reloadData()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int
{
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if section == 0
{
return BitArray.count
}
else
{
return WordArray.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelNameCell", for: indexPath) as! LabelNameTableViewCell
cell.LabelTitle.text = BitArray[indexPath.row]
if dict.count > 0{
cell.txtName.text = dict["M"+String(indexPath.row)] as? String ?? "null"
}
return cell
}
my tableviewCell
class LabelNameTableViewCell: UITableViewCell
{
#IBOutlet weak var LabelTitle: UILabel!
#IBOutlet weak var txtName: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
You can do somthing like this
#IBAction func saveButton(_ sender: UIBarButtonItem) {
var dict: [String:String] = [:]
for (i,bit) in bitArray.enumarate() {
let cell = tableView.cellForRow(at: IndexPath(row: i, section: 0)) as! LabelNameTableViewCell
dict[bit] = cell.txtName.text
}
for (i,word) in wordArray.enumarate() {
let cell = tableView.cellForRow(at: IndexPath(row: i, section: 1)) as! LabelNameTableViewCell
dict[word] = cell.txtName.text
}
// dict ---- "M0": "some text" ...
//then store it
}
p.s. In swift variables and function must start with lowercase character, see apple standard library functions.
My IBOutlet for songName is set up correctly, but for some reason
import UIKit
class QueueController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let testUser = Profile.init(username: "test", followers: [], following: [])
let songOne = Song.init(name: "S1", UWR: testUser.username, genre: "rock", artist: "s1")
var moreSongs = [Song]()
moreSongs = [Song(name: "gah", UWR: testUser.username, genre: "gahh", artist: "GAH")]
Queue.createQueue("2321x", creator: testUser, firstSong: songOne, additionalSongs: moreSongs)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Queue.theQueue!.count
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if let feed = Queue.theQueue {
return feed.count
} else {
return 0
}
}
func songIndex(cellIndex: Int) -> Int {
return tableView.numberOfSections - cellIndex - 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let song = Queue.theQueue![songIndex(indexPath.section)]
let cell = tableView.dequeueReusableCellWithIdentifier("queueCell", forIndexPath: indexPath) as! QueueCell
//cell.textLabel?.text = song.name this doesnt work also
cell.songName.text = "asdad"
return cell
}
}
For my Queue class:
import UIKit
class Queue {
let creator:Profile!
let planetID:String!
let beginningSong: Song!
var feed:Array<Song>?
static var theQueue:Array<Song>?
init (id:String!, creator:Profile!, firstSong:Song!) {
self.planetID = id
self.creator = creator
self.beginningSong = firstSong
}
func addSong (song: Song) {
feed?.append(song)
}
static func createQueue (id:String!, creator:Profile!, firstSong:Song!, additionalSongs: [Song]) {
let temp = Queue(id: id, creator: creator, firstSong: firstSong)
for song in additionalSongs {
temp.addSong(song)
}
self.theQueue = temp.feed
}
}
Song class:
import UIKit
class Song {
let name:String!
let userWhoRequested:String!
let genre: String?
let artist: String?
init (name:String!, UWR:String!, genre:String?, artist:String?) {
self.name = name
self.userWhoRequested = UWR
self.genre = genre
self.artist = artist
}
}
class QueueCell: UITableViewCell {
#IBOutlet weak var songName:UILabel!
#IBOutlet weak var userPicture:UIImageView!
#IBOutlet weak var isCurrent:UIImageView!
}
The table view displays properly but no data shows up in my controller, and I'm not sure why. The reuse cell identifier is also correct. The data in viewDidLoad() is also in my app delegate's func application.
Thanks!
Your problem is with the feed inside your CreateQueue method, that was never initialized
And change
var feed:Array<Song>?
to
var feed = [Song]()