All Items From Array Not In UITableView - swift

Below is the CatalogViewController, which holds a tableview. The tableview has 1 prototype cell, ShopCell. When I print the items in the loop, they print correct, but when shown in the table, items are missing.
(Removing the shuffle() method does nothing & removing removeDuplicates(), items appear more than once). I didn't include the addToFavorites(cell: ShopCell) because I'm testing it. It does nothing.
protocol ShopCellDelegate {
func addToFavorites(cell: ShopCell)
}
class ShopCell: UITableViewCell {
#IBOutlet weak var productImageView: UIImageView!
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
#IBOutlet weak var descTV: UITextView!
#IBOutlet weak var favoriteButton: UIButton!
var delegate: ShopCellDelegate?
override func prepareForReuse() {
super.prepareForReuse()
self.productImageView.image = nil
self.titleLabel.text = ""
self.priceLabel.text = ""
self.descTV.text = ""
self.favoriteButton.isHidden = true
}
func setProduct(product: Product) {
productImageView.sd_setImage(with: URL(string: product.urlToImage!), placeholderImage: UIImage(named: "1024ELP.png"))
titleLabel.text = product.itemName!
priceLabel.text = product.priceTag!
descTV.text = product.itemDesc!
}
#IBAction func favOrUnfav(_ sender: UIButton) {
if let delegate = self.delegate {
delegate.addToFavorites(cell: self)
}
}
}
//
class CatelogViewController: UIViewController, GADInterstitialDelegate, SFSafariViewControllerDelegate, UITableViewDelegate, UITableViewDataSource, ShopCellDelegate {
#IBOutlet weak var tableView: UITableView!
static var shopType = String()
static var linkToVisit = String()
var myProducts = [Product]()
var productKeys = [String]()
var interstitial: GADInterstitial!
override func viewWillAppear(_ animated: Bool) {
visuals() // Sets Nav Bar color & changes cell size if device == ipad
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.navigationController?.navigationBar.tintColor = UIColor.black
if CatelogViewController.shopType == "Apparel" {
self.title = NSLocalizedString("Shop Apparel", comment: "")
fetchProductLinks(child1: "ProductList", child2: "Products")
}else{
self.title = NSLocalizedString("Shop Others", comment: "")
fetchProductLinks(child1: "OtherList", child2: "OtherProducts")
//shuffleItems()
}
if let index = self.tableView.indexPathForSelectedRow{
self.tableView.deselectRow(at: index, animated: true)
}
}
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myProducts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ShopCell
let product = myProducts[indexPath.row]
cell.delegate = self
cell.favoriteButton.isHidden = true
cell.setProduct(product: product)
return cell
}
func fetchProductLinks(child1: String, child2: String) {
let ref = Database.database().reference()
let prodRef = ref.child(child1).child(child2)
prodRef.observeSingleEvent(of: .value, with: { snapshot in
self.myProducts.removeAll()
for items in snapshot.children {
let item = items as! DataSnapshot
let product = item.value as! [String : String]
let name = product["Name"]
let link = product["Link"]
let img = product["urlToImage"]
let desc = product["Description"]
let price = product["Price"]
let newProduct = Product(urlToImage: img, itemName: name, itemLink: link, itemDesc: desc, priceTag: price)
self.myProducts.append(newProduct)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
self.myProducts = self.shuffleArray(array: self.myProducts) as! [Product]
self.myProducts = self.myProducts.removeDuplicates()
})
ref.removeAllObservers()
}
extension Array where Element:Equatable {
func removeDuplicates() -> [Element] {
var result = [Element]()
for value in self {
if result.contains(value) == false {
result.append(value)
}
}
return result
}
}

You shuffle your array and you remove duplicates, but you don't reload data after it. So reload data of table view
self.myProducts = self.shuffleArray(array: self.myProducts) as! [Product]
self.myProducts = self.myProducts.removeDuplicates()
self.tableView.reloadData()

Related

How to add data into Firestore Swift by tapping on a cell

I have more restaurants , each have diferent food.Look here
This is how im retrieving data from the Firestore. In the previous controller I have a list of restaurants, each contains a list food.
struct Food {
var photoKeyRestaurant: String
var foodName: String
var foodDescription: String
var restaurantName: String
var priceFood: Int
}
class RestaurantViewController: UIViewController {
var restaurantName: String!
var food: [Food] = []
private let tableView: UITableView = {
let table = UITableView()
return table
}()
func getDatabaseRecords() {
let db = Firestore.firestore()
// Empty the array
food = []
db.collection("RestaurantViewController").whereField("restaurantName", isEqualTo: restaurantName).getDocuments { (snapshot, error) in
if let error = error {
print(error)
return
} else {
for document in snapshot!.documents {
let data = document.data()
let newEntry = Food(photoKeyRestaurant: data["photoKeyRestaurant"] as! String, foodName: data["foodName"] as! String, foodDescription: data["foodDescription"] as! String, restaurantName: data["restaurantName"] as! String , priceFood: data["priceLabel"] as! Int
)
self.food.append(newEntry)
}
}
DispatchQueue.main.async {
// self.datas = self.filteredData
self.tableView.reloadData()
}
}
}
How can I add the data of the selected cell by pressing on + to Firestore in this function ?
I'vrea create a protocol in my FoodTableViewCell , and I've called it in the RestaurantViewController.
func diddTapButtonCell(_ cell: FoodTableViewCell) {
let db = Firestore.firestore()
db.collection("cart").addDocument.(data: foodName) { (err) in
}
Edited: Added Table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return food.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FoodTableViewCell", for: indexPath) as! FoodTableViewCell
cell.delegate = self
let mancare = food[indexPath.row]
let storageRef = Storage.storage().reference()
let photoRef = storageRef.child(mancare.photoKeyRestaurant)
cell.foodImage.sd_setImage(with: photoRef)
cell.descriptionLabel.text = mancare.foodDescription
cell.foodNameLabel.text = mancare.foodName
cell.priceLabel.text = "\(mancare.priceFood) lei"
//Fac ca imaginea sa fie cerc - start
cell.foodImage.layer.borderWidth = 1
cell.foodImage.layer.masksToBounds = false
cell.foodImage.layer.borderColor = UIColor.black.cgColor
cell.foodImage.layer.cornerRadius = cell.foodImage.frame.height/2
cell.foodImage.clipsToBounds = true
//Fac ca imaginea sa fie cerc - finish
return cell
}
This is my tableview cell code
protocol CustomCellDelegate {
func diddTapButtonCell (_ cell: FoodTableViewCell)
}
class FoodTableViewCell: UITableViewCell {
var delegate: CustomCellDelegate?
#IBOutlet weak var foodImage: UIImageView!
#IBOutlet weak var foodNameLabel: UILabel!
#IBOutlet weak var descriptionLabel: UILabel!
#IBOutlet weak var priceLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
#IBAction func addToCart(_ sender: Any) {
delegate?.diddTapButtonCell(self)
}
}
https://firebase.google.com/docs/firestore/manage-data/add-data
And here is an example from one of my projects for adding data to firestore.
func updateDocument(rootCollection : String, doc: String, newValueDict: [String : Any], completion:#escaping (Bool) -> Void = {_ in }) {
let db = Firestore.firestore()
db.collection(rootCollection).document(doc).setData(newValueDict, merge: true){ err in
if let err = err {
print("Error writing document: \(err)")
completion(false)
}else{
completion(true)
}
}
}

How to retrieve an array of data from Firebase into iOS App

I am trying to retrieve this from my Firebase DB:
And this my VC, where I retrieve data from DB:
//
// VestibularesViewController_Design.swift
// newProject
//
// Created by Lucas Nascimento on 31/05/18.
// Copyright © 2018 Lucas Frazao. All rights reserved.
//
import UIKit
import Firebase
class VestibularesViewController_Design: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var topView: UIView!
#IBOutlet weak var tableView: UITableView!
#IBOutlet var backgroundView: UIView!
#IBOutlet weak var nomeVestibular: UILabel!
var ref: DatabaseReference!
var databaseHandle: DatabaseHandle?
var datas = [Datas]()
var newItems: [Datas] = []
var meses = ["Maio","Junho","Agosto", "Agosto"]
var ano = ["2018", ""]
var dias = ["18","20", "30", "31"]
var eventos = ["Inicio das inscrições", "Fim das inscricoes", "1ª Prova", "2ª Prova"]
override func viewDidLoad() {
super.viewDidLoad()
nomeVestibular.text = "ENEM"
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
loadPosts()
}
func numberOfSections(in tableView: UITableView) -> Int {
return datas.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func loadPosts() {
Database.database().reference().child("Datas").observe(.childAdded, with: { (snapshot) in
DispatchQueue.main.async {
self.tableView.reloadData();
}
if let dict = snapshot.value as? [String: [Any]] {
let diaText = dict["dia"] as? String
let mesText = dict["mes"] as? String
let eventoText = dict["evento"] as? String
// let dateText = dict["date"] as? String
let data = Datas(diaText: diaText, mesText: mesText, eventoText: eventoText)
self.newItems.append(data)
print(self.newItems)
}
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"cell", for: indexPath) as! VestibularesTableViewCell
//cell.titleNews.text = "O Edital do ENEM foi anunciado!"
ref = Database.database().reference()
cell.dia.text = datas[0].dia
cell.evento.text = datas[indexPath.row].evento
cell.mes.text = datas[indexPath.row].mes
if backgroundView.backgroundColor == UIColor.white {
cell.mes?.textColor = UIColor.black
cell.dia?.textColor = UIColor.black
cell.evento?.textColor = UIColor.black
}
return cell
}
override func viewWillDisappear(_ animated: Bool) {
//self.navigationController?.setNavigationBarHidden(false, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
If I try to load one value for each row, it works fine, but I can't get it to retrieve the array.
And this is where I created the class "Datas":
class Datas {
var dia: String?
var mes: String?
var evento: String?
init(diaText: String?, mesText: String?, eventoText: String?) {
dia = diaText
mes = mesText
evento = eventoText
}
}
I think something related to the class Datas needs to be changed in order for it to work properly.
You're trying to read the array into a string here:
let eventoText = dict["evento"] as? String
And that won't work. I'm not a Swift expert, but most likely it needs to be:
let eventoText = dict["evento"] as? [String]

How to access a property of a tableview cell from viewcontroller?

Please see screenshot. There is a repliesTableView, replyTextField and replyButtonin ViewController. repliesTableView cell is called ReplyCell. In ReplyCell there is a commentTableView to list all comments for that reply and a textfField, a commentButton to add new comments.
I have problem when add new replies and new comments. I guess I need to make comments array in ReplyCell empty when I click the Reply button. How can I make this happen? I have no idea how to access comments arrayfrom the root ViewController.
Exact problems: fter clicking commentButton, all comments in every cell doubled. After clicking replyButton, comments went to wrong cell.
Code:
import UIKit
import Firebase
class TopicForumVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
#IBOutlet weak var topicNameLabel: UILabel!
#IBOutlet weak var replyNumberLabel: UILabel!
#IBOutlet weak var repliesTableView: UITableView!
#IBOutlet weak var replyTextField: UITextField!
var topicName:String?
var firstKey:String?
var secondKey:String?
var replies = [String]()
var replyButtonTapped = false
override func viewDidLoad() {
super.viewDidLoad()
repliesTableView.delegate = self
repliesTableView.dataSource = self
replyTextField.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
topicNameLabel.text = self.topicName
loadReplies()
}
func loadReplies() {
self.replies = []
DataService.ds.Categories_Base.child(self.firstKey!).child("Topics").observe(.value, with:{(snapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
if let topicDict = snap.value as? Dictionary<String,AnyObject> {
if let topic = topicDict["text"] as? String {
if topic == self.topicName {
self.secondKey = snap.key
UserDefaults.standard.setValue(snap.key, forKey: Key_SecondKey)
if let replyDict = topicDict["replies"] as? Dictionary<String,AnyObject> {
for eachDict in replyDict {
if let textDict = eachDict.value as? Dictionary<String,AnyObject> {
if let reply = textDict["text"] as? String {
self.replies.append(reply)
self.replyNumberLabel.text = String(self.replies.count)
}
}
}
}
}
}
}
}
self.repliesTableView.reloadData()
}
})
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return replies.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "ReplyCell") as? ReplyCell {
let reply = replies[indexPath.row]
cell.configureReplyCell(reply: reply)
return cell
} else {
return UITableViewCell()
}
}
#IBAction func replyButtonTapped(_ sender: Any) {
replyButtonTapped = true
if let reply = replyTextField.text, reply != "" {
self.replies = []
DataService.ds.Categories_Base.child(self.firstKey!).child("Topics").child(self.secondKey!).child("replies").childByAutoId().child("text").setValue(reply)
self.repliesTableView.reloadData()
let i = replies.count
for n in 0..<i {
let indexPath = IndexPath(row: n, section: 1)
let cell = repliesTableView.cellForRow(at: indexPath) as! ReplyCell
cell.comments = []
cell.repliesToReplyTableView.reloadData()
}
self.replyTextField.text = ""
self.replyButtonTapped = false
}
}
}
import UIKit
import Firebase
class ReplyCell: UITableViewCell,UITableViewDataSource,UITableViewDelegate, UITextFieldDelegate {
#IBOutlet weak var replyTextView: UITextView!
#IBOutlet weak var repliesToReplyTableView: UITableView!
#IBOutlet weak var commentTextField: UITextField!
var reply:String?
var comments = [String]()
var replyKey:String?
override func awakeFromNib() {
super.awakeFromNib()
self.comments = []
repliesToReplyTableView.delegate = self
repliesToReplyTableView.dataSource = self
commentTextField.delegate = self
loadComments()
}
func configureReplyCell(reply:String) {
self.reply = reply
self.replyTextView.text = self.reply
}
func loadComments() {
self.comments = []
if let firstKey = UserDefaults.standard.value(forKey: Key_FirstKey) as? String, let secondKey = UserDefaults.standard.value(forKey: Key_SecondKey) as? String {
DataService.ds.Categories_Base.child(firstKey).child("Topics").child(secondKey).child("replies").observe(.value, with:{(snapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
if let replyDict = snap.value as? Dictionary<String,AnyObject> {
if let reply = replyDict["text"] as? String {
if reply == self.reply {
self.replyKey = snap.key
DataService.ds.Categories_Base.child(firstKey).child("Topics").child(secondKey).child("replies").child(snap.key).child("comments").observe(.value, with: { (commentSnapshot) in
if let commentSnapshots = commentSnapshot.children.allObjects as? [FIRDataSnapshot] {
for commentSnap in commentSnapshots {
if let commentDict = commentSnap.value as? Dictionary<String,AnyObject> {
if let comment = commentDict["text"] as? String {
self.comments.append(comment)
}
}
}
}
self.repliesToReplyTableView.reloadData()
})
}
}
}
}
}
})
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return comments.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let commentCell = tableView.dequeueReusableCell(withIdentifier:"CommentCell")
commentCell?.textLabel?.text = comments[indexPath.row]
return commentCell!
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
#IBAction func commentBtnPressed(_ sender: Any) {
if let comment = commentTextField.text, comment != "" {
self.comments = []
if let firstKey = UserDefaults.standard.value(forKey: Key_FirstKey) as? String, let secondKey = UserDefaults.standard.value(forKey: Key_SecondKey) as? String {
DataService.ds.Categories_Base.child(firstKey).child("Topics").child(secondKey).child("replies").child(self.replyKey!).child("comments").childByAutoId().child("text").setValue(comment)
if let myViewController = parentViewController as? TopicForumVC {
// myViewController.repliesTableView.reloadData()
myViewController.replies = []
}
self.repliesToReplyTableView.reloadData()
self.commentTextField.text = ""
self.replyKey = ""
}
}
}
I don't really know the exact circumstances of what you're building but there are two ideas that may offer some guidance.
1) If your table is displaying content from a data source then you will likely have some kind of reference. E.g. when loading the cells (in this case CustomCell) you'll do something like get the index of the cell and get the same index from the data, and put that data in the cells content. If that's the case, all you have to do on the button click is use tableview.cellForRowAtIndexPath with your sender object, and then remove the array from the data source, e.g. tableDataSource[index] = nil and reload the tableView.
2) If you have a stored property on the CustomCell that you've add specifically for storing this array, then you'd cast the sender object to CustomCell and remove the property, as in Kim's answer.
Hope this helps, but without more information it's kind of hard to tell.
let cell = tableview.cellForRowAtIndexPath(...) as? CustomCell
if cell != nil {
let arr = cell.array
}
BTW: I would re-think storing your array in the cell..

How to sort an array from multiple textfields with one property

I was looking for an answer for this question but I couldn't find anything. I'm new in swift programming and also in stackoverflow, so I hope anyone can help me.
I try to make an app with an "EditViewController" which is there with multiple textfields for name, prename, etc.
I'm able to save this "phonebook" entries, but I'm not able to sort the array by the property name.
The editing interface is only this code:
import UIKit
class EditViewController: UIViewController {
#IBOutlet weak var vornameTextField: UITextField!
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var adresseTextField: UITextField!
#IBOutlet weak var hausnummerTextField: UITextField!
#IBOutlet weak var plzTextField: UITextField!
#IBOutlet weak var ortTextField: UITextField!
#IBOutlet weak var telefonnummerTextField: UITextField!
#IBOutlet weak var berufTextField: UITextField!
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
The code for saving the textfields is:
import UIKit
class Telefonbuch: NSObject, NSCoding
{
var name: String!
var vorname: String!
var beruf: String!
var telefonnummer: String!
var adresse: String!
var hausnummer: String!
var plz: String!
var ort: String!
init(name: String, vorname: String, beruf:String, telefonnummer: String, adresse: String, hausnummer: String, plz: String, ort: String)
{
self.name = name
self.vorname = vorname
self.beruf = beruf
self.telefonnummer = telefonnummer
self.adresse = adresse
self.hausnummer = hausnummer
self.plz = plz
self.ort = ort
}
required init?(coder aDecoder: NSCoder) {
name = aDecoder.decodeObjectForKey("name") as? String
vorname = aDecoder.decodeObjectForKey("vorname") as? String
beruf = aDecoder.decodeObjectForKey("beruf") as? String
telefonnummer = aDecoder.decodeObjectForKey("telefonnummer") as? String
adresse = aDecoder.decodeObjectForKey("adresse") as? String
hausnummer = aDecoder.decodeObjectForKey("hausnummer") as? String
plz = aDecoder.decodeObjectForKey("plz") as? String
ort = aDecoder.decodeObjectForKey("ort") as? String
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: "name")
aCoder.encodeObject(vorname, forKey: "vorname")
aCoder.encodeObject(beruf, forKey: "beruf")
aCoder.encodeObject(telefonnummer, forKey: "telefonnummer")
aCoder.encodeObject(adresse, forKey: "adresse")
aCoder.encodeObject(hausnummer, forKey: "hausnummer")
aCoder.encodeObject(plz, forKey: "plz")
aCoder.encodeObject(ort, forKey: "ort")
}
static func saveArray(data: [Telefonbuch])
{
if data.count == 0 {return}
if let path = getFilePath() {
NSKeyedArchiver.archiveRootObject(data, toFile: path)
}
}
static func loadArray() -> [Telefonbuch]
{
if let path = getFilePath() {
if let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? [Telefonbuch]
{
return result
}
}
return [Telefonbuch]()
}
private static func getFilePath() -> String?
{
let pfd = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
if let pfad = pfd.first {
return pfad + "Kontakte.bin"
}else {
return nil
}
}
}
Now I would like to make the tableview, where I display the data, sorted and with header. But I'm not able to sort the [Telefonbuch] Array by property name.
import UIKit
class OverViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var addButton: UIBarButtonItem!
#IBOutlet weak var editButton: UINavigationItem!
var kontaktListe = Telefonbuch.loadArray()
override func viewDidLoad()
{
super.viewDidLoad()
tableView.dataSource = self
tableView.reloadData()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func addButtonPressed(sender: AnyObject)
{
performSegueWithIdentifier("editSegue", sender: self)
}
#IBAction func returnToMainController(segue: UIStoryboardSegue)
{
if let scr = segue.sourceViewController as? EditViewController
{
let newvorname = scr.vornameTextField.text
let newname = scr.nameTextField.text
let newadresse = scr.adresseTextField.text
let newhausnummer = scr.hausnummerTextField.text
let newpostleitzahl = scr.plzTextField.text
let newort = scr.ortTextField.text
let newtelefonnummer = scr.telefonnummerTextField.text
let newberuf = scr.berufTextField.text
let newKontakt = Telefonbuch(name: newname!, vorname: newvorname!, beruf: newberuf!, telefonnummer: newtelefonnummer!, adresse: newadresse!, hausnummer: newhausnummer!, plz: newpostleitzahl!, ort: newort!)
kontaktListe.insert(newKontakt, atIndex: 0)
Telefonbuch.saveArray(kontaktListe)
tableView.reloadData()
}
}
extension OverViewController: UITableViewDataSource
{
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 3
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return kontaktListe.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("ProtoCell", forIndexPath: indexPath)
let row = indexPath.row
cell.textLabel?.text = kontaktListe[row].name + " " + kontaktListe[row].vorname
cell.detailTextLabel?.text = kontaktListe[row].ort
return cell
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return true
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
{
if editingStyle == .Delete
{
kontaktListe.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.reloadData()
}
}
extension OverViewController: UITableViewDelegate
{
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
performSegueWithIdentifier("detailSegue", sender: self)
}
}
Can anyone help me because I don't find a solution.
Thanks in advance.
You should be able to sort the array with:
kontaktListe.sort({ $0.name > $1.name })

How can I return multiple elements in a cell?

I would like to return multiple elements of a cell. How can I do it? This is the code I'm working on
//
// TableViewControllerCompiti.swift
// Secondo B
//
// Created by Edoardo on 23/12/15.
// Copyright © 2015 ERC. All rights reserved.
//
import UIKit
import Parse
class TableViewControllerCompiti: UITableViewController {
var selfTable: NSMutableArray = NSMutableArray()
#IBOutlet var MessageTable: UITableView!
#IBOutlet weak var Compiti: UILabel!
#IBOutlet weak var DescrizioneCompiti: UITextView!
#IBOutlet weak var DataCompiti: UILabel!
var messagesArray: [String] = [String]()
var descriptionArray: [String] = [String]()
var dateArray: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
func retrieveMessages() {
let query = PFQuery(className: "Compiti")
query.findObjectsInBackgroundWithBlock {
(remoteObjects: [PFObject]?, error: NSError?) -> Void in
for messageObject in remoteObjects! {
let messageText: String? = (messageObject as PFObject) ["Materia"] as? String
let descriptionText: String? = (messageObject as PFObject) ["Compiti"] as? String
let date: String? = (messageObject as PFObject) ["Data"] as? String
if messageText != nil {
self.messagesArray.append(messageText!)
}
if descriptionText != nil {
self.descriptionArray.append(descriptionText!)
}
if date != nil {
self.dateArray.append(date!)
}
}
self.MessageTable.reloadData()
}
}
retrieveMessages()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell?()
cell = tableView.dequeueReusableCellWithIdentifier("cell")
Compiti.text = self.messagesArray[indexPath.row]
DataCompiti.text = self.dateArray[indexPath.row]
DescrizioneCompiti.text = self.descriptionArray[indexPath.row]
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "ReUseCell")
}
return cell!
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.messagesArray.count
}
}
When I run it, the simulator just returns a bunch of empty cells. How can I do to return the elements?
Remark From your code:
you cannot implement and call the function inside of viewDidLoad(), you implement all functions/methods outside of the life cycle then call inside.
I don't think its a good idea of having three different arrays to contain your data,you should learn struct or class data structures to group your data since you are getting them from the same class.
struct myDataContainer
{
var message:String?
var descriptionText:String?
var date:String?
}
var arrayOfContainers = [myDataContainer]() //<-- this will be your new array
I saw that you had a customize cell where you wanted to have 2 Labels and UITextView.. you should create a subclass of UITableViewCell then group them inside.
class CustomizeCell :UITableViewCell
{
#IBOutlet weak var Compiti: UILabel!
#IBOutlet weak var DescrizioneCompiti: UITextView!
#IBOutlet weak var DataCompiti: UILabel!
} //<--- Subclass of UITableViewCell above
struct myDataContainer
{
var message:String?
var descriptionText:String?
var date:String?
}
class TableViewControllerCompiti: UITableViewController {
var arrayOfContainers = [myDataContainer]() //<-- this will be your new array
override func viewDidLoad()
{
super.viewDidLoad()
retrieveMessages() //<--- call function
}
func retrieveMessages()
{
var objectT = myDataContainer()
let query = PFQuery(className: "Compiti")
query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in
if let remoteObjects = objects
{
for messageObject in remoteObjects
{
let messageText = messageObject["Materia"] as? String
let description = messageObject["Compiti"] as? String
let date = messageObject["Data"] as? String
objectT.message = messageText!
objectT.descriptionText = description!
objectT.date = date!
self.arrayOfContainers.append(objectT)
}
self.tableView.reloadData()
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomizeCell
// as! CustomizeCell technically speaking make sure that we access our customize cell
let data = self.arrayOfContainers[indexPath.row]
cell.Compiti.text = data.message
cell.DataCompiti.text = data.descriptionText
cell.DescrizioneCompiti.text = data.date
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arrayOfContainers.count
}
}