I want to animate table view cell during deleting the row.I am trying the following code, but the cell animation is not very good. How could i improve the animatin please help.
My code is:
#objc func userLikeButtonWasTappaed(sender: UIButton){
guard let indexPath = tableView.indexPathForRow(at: sender.convert(sender.frame.origin, to: tableView)) else {
return
}
let cell = tableView.cellForRow(at: indexPath) as? MatchingUsersTVCell
let tag = sender.tag
if modelNameArray.count > 0{
let userid = userIdArray[tag]
totalScoreArray.remove(at: tag)
modelNameArray.remove(at: tag)
self.tableView.beginUpdates()
UIView.animate(withDuration: 1) {
self.tableView.deleteRows(at: [indexPath], with: .right)
}
self.tableView.endUpdates()
let uid: Int = UserDefaults.standard.value(forKey: "User_Id") as! Int
let accessToken: String = UserDefaults.standard.value(forKey: "access_token") as! String
apiRequest.likeTheUser(uid, userid, accessToken) { (likedUser) in
}
}
}
Related
I am a beginner and am trying to tweak the EmojiDictionary (page # 556) project from Apple's App Development with Swift book by changing the one-dimensional data array to two-dimensional.
Here's the original implementation:
Data Class:
class Emoji {
var symbol: String
var name: String
var description: String
var usage: String
init(symbol: String, name: String, description: String, usage: String)
{
self.symbol = symbol
self.name = name
self.description = description
self.usage = usage
}
}
Original Data Structure: An array of emojis:
var emojis: [Emoji] = [...]
#IBAction func unwindToEmojiTableView(segue: UIStoryboardSegue) {
guard segue.identifier == "saveUnwind" else { return }
let sourceViewController = segue.source as! AddEditEmojiTableViewController
if let emoji = sourceViewController.emoji {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
emojis[selectedIndexPath.row] = emoji
tableView.reloadRows(at: [selectedIndexPath], with: .none)
} else {
let newIndexPath = IndexPath(row: emojis.count, section: 0)
emojis.append(emoji)
tableView.insertRows(at: [newIndexPath], with: .automatic)
}
}
}
And here's what I am trying to do:
My Data Structure: Array of arrays:
var emojis: [[Emoji]] = [ [Emoji(...), Emoji(...), ...], [Emoji(...), etc...] ]
#IBAction func unwindToEmojiTableView(segue: UIStoryboardSegue) {
guard segue.identifier == "saveUnwind" else { return }
let sourceViewController = segue.source as! AddEditEmojiTableViewController
if let emoji = sourceViewController.emoji {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
emojis[selectedIndexPath.row][selectedIndexPath.row] = emoji
tableView.reloadRows(at: [selectedIndexPath], with: .none)
} else {
let newIndexPath = IndexPath(row: ?, section: 0)
emojis[?].append(emoji)
tableView.insertRows(at: [newIndexPath], with: .automatic)
}
}
}
But I can't figure out how to get it to work. The code (with question marks) in the block quote is where I am stuck. Please help.
let newIndexPath = IndexPath(row: selectedIndexPath.row, section: selectedIndexPath.section)
emojis[selectedIndexPath.section].append(emoji)
If you are trying to append to the object that is selected for a specific section.
My tableview currently updates my table and adds new items in real-time when they are added to my firebase database. The problem is that I cannot delete in real-time. I am storing my data from firebase in a local array, and then loading that array to the tableview.
I tried to condense my code a bit. I also tried to put the Firebase code that is inside my removeDeletedItems() function inside my populateArrays() function, and to put it after the .childAdded listener, but did not have luck with deleting the data in real-time.
override func viewDidLoad() {
super.viewDidLoad()
populateArrays()
}
func removeDeletedItems() {
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Users").observe(FIRDataEventType.childRemoved, with: { (FIRDataSnapshot) in
guard let emailToFind = FIRDataSnapshot.value as? String else { return }
for (index, email) in self.usernames.enumerated() {
if email == emailToFind {
let indexPath = IndexPath(row: index, section: 0)
self.usernames.remove(at: index)
self.tableView.deleteRows(at: [indexPath], with: .fade)
self.tableView.reloadData()
}
}
})
}
func populateArrays(){
let databaseRef = FIRDatabase.database().reference()
databaseRef.child("Users").observe(FIRDataEventType.childAdded, with: { (FIRDataSnapshot) in
if let data = FIRDataSnapshot.value as? NSDictionary {
if let name = data[Constants.NAME] as? String {
self.usernames.append(name)
self.removeDeletedItems()
self.tableView.reloadData()
}
}
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = usernames[indexPath.row]
return cell
}
Isn't the observed value always a dictionary? And shouldn't you check also for the name rather than the email?
The loop to find the name is not needed. There is a convenience function.
databaseRef.child("Users").observe(FIRDataEventType.childRemoved, with: { snapshot in
guard let data = snapshot.value as? [String:Any],
let nameToFind = data[Constants.NAME] as? String else { return }
if let index = self.usernames.index(of: nameToFind) {
let indexPath = IndexPath(row: index, section: 0)
self.usernames.remove(at: index)
self.tableView.deleteRows(at: [indexPath], with: .fade)
// don't reload the table view after calling `deleteRows`
}
}
})
Problem here is that I'm getting duplicates in my table view and I know why but I don't know how to fix it and implement a different system.
My app is a blog reader that reads from a MYSQL database using PHP to send JSON to my Swift app. My table view has two sections, one for all the objects from the database and second section is for when I click the follow button on the cells, basically move the objects from mainArray to followedArray. Each section is using an array so for example I move all the objects from mainArray to followedArray and I update the table I get all those objects again in mainArray, obviously because the mainarray is empty and the code is just doing its job.
So how can I implement a better system so when the user moves the objects from one section to another (or from mainArray to followedArray) mainArray doesn't get repopulated with the same objects that it had and are now in followedArray.
Here is the code I use.
MainController.swift - Class where Tableview is at
var mainArray = [Blog]()
var followedArray = [Blog]()
// Title for Header
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if !(searchController.isActive && searchController.searchBar.text != "") {
if section == 0 {
return "Followed Blogs"
}
else {
return "All Blogs"
}
}
return "Filtered Blogs"
}
// Number of Rows in Section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if !(searchController.isActive && searchController.searchBar.text != "") {
if section == 0 {
return followedArray.count
}
else if (section == 1) {
return mainArray.count
}
}
return filteredArray.count
}
// CellForRowAt indexPath
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let CellIdentifier = "Cell"
var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell
if cell != cell {
cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier)
}
// Configuring the cell
var blogObject: Blog
if !(searchController.isActive && searchController.searchBar.text != "") {
if indexPath.section == 0 {
blogObject = followedArray[indexPath.row]
cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self)
}
else if indexPath.section == 1 {
blogObject = mainArray[indexPath.row]
cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
}
}
else {
blogObject = filteredArray[indexPath.row]
cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
}
return cell
}
// Follow Button
#IBAction func followButtonClick(_ sender: UIButton!) {
// Adding row to tag
let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) {
// Showing Status Labels
let cell = self.myTableView.cellForRow(at: indexPath) as! CustomCell
cell.firstStatusLabel.isHidden = false
cell.secondStatusLabel.isHidden = false
// Change Follow to Following
(sender as UIButton).setImage(UIImage(named: "follow.png")!, for: .normal)
cell.followButton.isHidden = true
cell.followedButton.isHidden = false
// Checking wether to import from mainArray or filteredArray to followedArray
if !(searchController.isActive && searchController.searchBar.text != "") {
self.myTableView.beginUpdates()
// ----- Inserting Cell to followedArray -----
followedArray.insert(mainArray[indexPath.row], at: 0)
myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
// ----- Removing Cell from mainArray -----
mainArray.remove(at: indexPath.row)
let rowToRemove = indexPath.row
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)
self.myTableView.endUpdates()
myTableView.reloadData()
// After Updating Table, Save the Archived to UserDefaults
saveUserDefaults()
}
else {
self.myTableView.beginUpdates()
// ----- Inserting Cell to followedArray -----
let blogObject: Blog = filteredArray[indexPath.row]
let indexOfObjectInArray = mainArray.index(of: blogObject)
followedArray.insert(blogObject, at: 0)
// ----- Removing Cell from filteredArray -----
filteredArray.remove(at: indexPath.row)
mainArray.remove(at: indexOfObjectInArray!)
let rowToRemove = indexPath.row
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)
self.myTableView.endUpdates()
myTableView.reloadData()
// After Updating Table, Save the Archived to UserDefaults
saveUserDefaults()
}
}
}
// Retrieving Data from Server
func retrieveDataFromServer() {
let getDataURL = "http://example.com/receiving.php"
let url: NSURL = NSURL(string: getDataURL)!
do {
let data: Data = try Data(contentsOf: url as URL)
let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray
// Looping through jsonArray
for jsonObject in jsonArray {
if let blog = Blog.createGame(from: jsonObject as AnyObject) {
mainArray.append(blog)
}
}
}
catch {
print("Error: (Retrieving Data)")
}
myTableView.reloadData()
}
Blog.swift - Handles the blogs objects where I user NSCoder too
import UIKit
class Blog: NSObject, NSCoding {
var blogName: String!
var blogStatus1: String!
var blogStatus2: String!
var blogURL: String!
var blogID: String!
var blogType: String!
var blogDate: String!
var blogPop: String!
static func createBlog(from jsonObject: AnyObject) -> Blog? {
guard let bID: String = jsonObject.object(forKey: "id") as? String,
let bName: String = jsonObject.object(forKey: "blogName") as? String,
let bStatus1: String = jsonObject.object(forKey: "blogStatus1") as? String,
let bStatus2: String = jsonObject.object(forKey: "blogStatus2") as? String,
let bURL: String = jsonObject.object(forKey: "blogURL") as? String,
let bType: String = jsonObject.object(forKey: "blogType") as? String,
let bDate: String = jsonObject.object(forKey: "blogDate") as? String,
let bPop: String = jsonObject.object(forKey: "blogPop") as? String
else {
print("Error: (Creating Blog Object)")
return nil
}
let blog = Blog()
blog.blogName = bName
blog.blogStatus1 = bStatus1
blog.blogStatus2 = bStatus2
blog.blogURL = bURL
blog.blogID = bID
blog.blogType = bType
blog.blogDate = bDate
blog.blogPop = bPop
return blog
}
convenience required init?(coder aDecoder: NSCoder) {
self.init ()
self.blogName = aDecoder.decodeObject(forKey: "blogName") as! String
self.blogStatus1 = aDecoder.decodeObject(forKey: "blogStatus1") as! String
self.blogStatus2 = aDecoder.decodeObject(forKey: "blogStatus2") as! String
self.blogURL = aDecoder.decodeObject(forKey: "blogURL") as! String
self.blogID = aDecoder.decodeObject(forKey: "blogID") as! String
self.blogType = aDecoder.decodeObject(forKey: "blogType") as! String
self.blogDate = aDecoder.decodeObject(forKey: "blogDate") as! String
self.blogPop = aDecoder.decodeObject(forKey: "blogPop") as! String
}
func encode(with aCoder: NSCoder) {
aCoder.encode(blogName, forKey: "blogName")
aCoder.encode(blogStatus1, forKey: "blogStatus1")
aCoder.encode(blogStatus2, forKey: "blogStatus2")
aCoder.encode(blogURL, forKey: "blogURL")
aCoder.encode(blogID, forKey: "blogID")
aCoder.encode(blogType, forKey: "blogType")
aCoder.encode(blogDate, forKey: "blogDate")
aCoder.encode(blogPop, forKey: "blogPop")
}
}
Is there a way to check before repopulating mainArray to see whats in followedArray and whatever is missing or added to the database to import and not create duplicates because new blogs will be added and the users will transfer blogs across sections so this is a major issue that I am having.
Would appreciate the help as I am still learning Swift, thank you.
I would recommend saving an unique identifier for the blogs which should be in the followed category into an array and on each reload of the tableView, move the proper cells to the correct section.
You seem to be using UserDefaults but have no modifications to them. Using my method the only array that is required to be saved into and loaded from the UserDefaults is the list of followed blogs. The rest default to the main list, even as new blogs show up.
you will need one more array:
var mainArray = [Blog]()
var followedArray = [Blog]()
var followedIdentifiers = [String]()
or whatever datatype the identifier will be in
You could also use a Set as you want no duplicates in the followedIdentifiers
var followedIdentifiers = Set<String>()
Here are modifications to the relevant parts of your code (My changes marked with <----):
// Checking whether to import from mainArray or filteredArray to followedArray
if !(searchController.isActive && searchController.searchBar.text != "") {
self.myTableView.beginUpdates()
// Save identifier into followedIdentifier array <--------------
self.followedIdentifiers.insert(mainArray[indexPath.row].blogID)
// ----- Inserting Cell to followedArray -----
followedArray.insert(mainArray[indexPath.row], at: 0)
myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
// ----- Removing Cell from mainArray -----
mainArray.remove(at: indexPath.row)
let rowToRemove = indexPath.row
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)
self.myTableView.endUpdates()
myTableView.reloadData()
// After Updating Table, Save the Archived to UserDefaults
saveUserDefaults()
} else {
self.myTableView.beginUpdates()
// Remove identifier into followedIdentifier array <------------
self.followedIdentifiers.remove(followedArray[indexPath.row].blogID)
// ----- Inserting Cell to followedArray -----
let blogObject: Blog = filteredArray[indexPath.row]
let indexOfObjectInArray = mainArray.index(of: blogObject)
followedArray.insert(blogObject, at: 0)
// ----- Removing Cell from filteredArray -----
filteredArray.remove(at: indexPath.row)
mainArray.remove(at: indexOfObjectInArray!)
let rowToRemove = indexPath.row
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)
self.myTableView.endUpdates()
myTableView.reloadData()
// After Updating Table, Save the Archived to UserDefaults
saveUserDefaults()
}
// Retrieving Data from Server
func retrieveDataFromServer() {
let getDataURL = "http://example.com/receiving.php"
let url: NSURL = NSURL(string: getDataURL)!
do {
let data: Data = try Data(contentsOf: url as URL)
let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray
// Clear the arrays <-------------
self.followedArray = [Blog]()
self.mainArray = [Blog()]
// Looping through jsonArray
for jsonObject in jsonArray {
if let blog = Blog.createBlog(from: jsonObject as AnyObject) {
// Check if identifiers match <------------
if followedIdentifiers.contains(blog.blogID) {
self.followedArray.append(blog)
} else {
self.mainArray.append(blog)
}
}
}
} catch {
print("Error: (Retrieving Data)")
}
myTableView.reloadData()
}
In order for this to work across sessions you must have something similar to this in your saveUserDefaults()
UserDefaults.standard.setValue(Array(self.followedIdentifiers), forKey: "someName")
and this where you load from UserDefaults
self.followedIdentifiers = Set(UserDefaults.standard.stringArray(forKey: "someName"))
Using Swift 3
I've been working on a blog reader app for a while but I've been stuck on how to save the tableview like how the user left it.
The app works by me inputing data into the mysql database which is sent using php and received with json and swift 3 to populate the tableview. I expect to update this database everyday. There are two sections in the tableview, top one is followedArray and second is mainArray which is where all the objects are after being in jsonArray. When the user clicks the follow button, that single cell is moved into followedArray where the user can see the blog being updated and receive notifications.
I've tried using UserDefaults by saving the whole array into an object but it didn't work out as it was way to complicated creating the NSData objects using NSCoding, when there is no need to save the entire array (my guess).
My question, is there a simple way that I can save how the user left the tableview?
For example: In the 'All Objects Section' lets say I have 3 cells and the user clicks the follow button on 2 cells, those 2 cells are now in the 'Followed Section' with 1 cell in 'All Objects Section'. How do I save the state of this tableview. I want to save in which section the followed cell is in now and the changed button which is now a Followed Button instead of Follow Button (saving the hidden enabled code as I hide one button to show the other).
Do I use UserDefaults or CoreData? Maybe a CocoaPod library? Do I save the entire array or just which section its in and the .hidden code? How will updating and adding new objects affect it?
I'm fairly new to Swift so if you can help it would be appreciated. Any code that you need to understand how the app works, just ask for it as I don't know what code is necessary to show. I know you're not supposed to ask for code but if you can it would be great as I've been trying for months with no progress. Thank you!
Here is how I handle incoming json data:
MainController.swift
var jsonArray: NSMutableArray = []
var mainArray = [Blog]()
var followedArray = [Blog]()
var filteredArray = [Blog]()
// Title for Header
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if !(searchController.isActive && searchController.searchBar.text != "") {
if section == 0 {
return "Followed Blogs"
}
else {
return "All Blogs"
}
}
return "Filtered Blogs"
}
// Number of Rows in Section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if !(searchController.isActive && searchController.searchBar.text != "") {
if section == 0 {
return followedArray.count
}
else if (section == 1) {
return mainArray.count
}
}
return filteredArray.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let CellIdentifier = "Cell"
var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell
if cell != cell {
cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier)
}
// Configuring the cell
var blogObject: Blog
if !(searchController.isActive && searchController.searchBar.text != "") {
if indexPath.section == 0 {
blogObject = followedArray[indexPath.row]
cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self)
}
else if indexPath.section == 1 {
blogObject = mainArray[indexPath.row]
cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
}
}
else {
blogObject = filteredArray[indexPath.row]
cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
}
return cell
}
#IBAction func followButtonClick(_ sender: UIButton!) {
// Adding row to tag
let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) {
// Showing Status Labels
let cell = self.myTableView.cellForRow(at: indexPath) as! CustomCell
cell.firstStatusLabel.isHidden = false
cell.secondStatusLabel.isHidden = false
// Change Follow to Following
(sender as UIButton).setImage(UIImage(named: "follow.png")!, for: .normal)
cell.followButton.isHidden = true
cell.followedButton.isHidden = false
// Checking wether to import from mainArray or filteredArray to followedArray
if !(searchController.isActive && searchController.searchBar.text != "") {
self.myTableView.beginUpdates()
// ----- Inserting Cell to followedArray -----
followedArray.insert(mainArray[indexPath.row], at: 0)
myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
// ----- Removing Cell from mainArray -----
mainArray.remove(at: indexPath.row)
let rowToRemove = indexPath.row
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)
self.myTableView.endUpdates()
myTableView.reloadData()
}
else {
self.myTableView.beginUpdates()
// ----- Inserting Cell to followedArray -----
let blogObject: Blog = filteredArray[indexPath.row]
let indexOfObjectInArray = mainArray.index(of: blogObject)
followedArray.insert(blogObject, at: 0)
// ----- Removing Cell from filteredArray -----
filteredArray.remove(at: indexPath.row)
blogArray.remove(at: indexOfObjectInArray!)
let rowToRemove = indexPath.row
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)
self.myTableView.endUpdates()
myTableView.reloadData()
}
}
}
func retrieveDataFromServer() {
let getDataURL = "http://exampleblog.com/receiving.php"
let url: NSURL = NSURL(string: getDataURL)!
do {
let data: Data = try Data(contentsOf: url as URL)
jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray
// Looping through jsonArray
for i in 0..<jsonArray.count {
// Create Blog Object
let bID: String = (jsonArray[i] as AnyObject).object(forKey: "id") as! String
let bName: String = (jsonArray[i] as AnyObject).object(forKey: "blogName") as! String
let bStatus1: String = (jsonArray[i] as AnyObject).object(forKey: "blogStatus1") as! String
let bStatus2: String = (jsonArray[i] as AnyObject).object(forKey: "blogStatus2") as! String
let bURL: String = (jsonArray[i] as AnyObject).object(forKey: "blogURL") as! String
// New
let bType: String = (jsonArray[i] as AnyObject).object(forKey: "blogType") as! String
let bDate: String = (jsonArray[i] as AnyObject).object(forKey: "blogDate") as! String
let bPop: String = (jsonArray[i] as AnyObject).object(forKey: "blogPop") as! String
// Add Blog Objects to mainArray
mainArray.append(Blog(blogName: bName, andBlogStatus1: bStatus1, andBlogStatus2: bStatus2, andBlogURL: bURL, andBlogID: bID, andBlogType: bType, andBlogDate: bDate, andBlogPop: bPop))
}
}
catch {
print("Error: (Retrieving Data)")
}
myTableView.reloadData()
}
CustomCell.swift - How the cells are being populatedenter code here
class CustomCell: UITableViewCell {
#IBOutlet weak var firstStatusLabel: UILabel!
#IBOutlet weak var secondStatusLabel: UILabel!
#IBOutlet weak var followButton: UIButton!
#IBOutlet weak var followedButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
self.followButton.isHidden = true
self.followedButton.isHidden = true
}
func populateCell(_ blogObject: Blog, isFollowed: Bool, indexPath: IndexPath, parentView: Any) {
// Loading Status Labels
self.firstStatusLabel.text = blogObject.blogStatus1
self.secondStatusLabel.text = blogObject.blogStatus2
self.firstStatusLabel.isHidden = true
self.secondStatusLabel.isHidden = true
if isFollowed {
self.followedButton.tag = indexPath.row
self.followedButton.addTarget(parentView, action: #selector(MainController.followedButtonClick(_:)), for: .touchUpInside)
self.followedButton.isHidden = false
self.followButton.isHidden = true
// Status Labels
self.firstStatusLabel.isHidden = false
self.secondStatusLabel.isHidden = false
}
else {
self.followButton.tag = indexPath.row
self.followButton.addTarget(parentView, action: #selector(MainController.followButtonClick(_:)), for: .touchUpInside)
self.followedButton.isHidden = true
self.followButton.isHidden = false
// Status Labels
self.firstStatusLabel.isHidden = true
self.secondStatusLabel.isHidden = true
}
}
}
Blog.swift
class Blog: NSObject {
// Strings
var blogName: String?
var blogStatus1: String?
var blogStatus2: String?
var blogURL: String?
var blogID: String?
var blogType: String?
var blogDate: String?
var blogPop: String?
override init() {
}
// Converting Strings into Objects
init(blogName bName: String,
andBlogStatus1 bStatus1: String,
andBlogStatus2 bStatus2: String,
andBlogURL bURL: String,
andBlogID bID: String,
andBlogType bType: String,
andBlogDate bDate: String,
andBlogPop bPop: String)
{
super.init()
self.blogName = bName
self.blogStatus1 = bStatus1
self.blogStatus2 = bStatus2
self.blogURL = bURL
self.blogID = bID
self.blogType = bType
self.blogDate = bDate
self.blogPop = bPop
}
}
There are lots of different ways to accomplish what you want, both storing the information locally or remotely. If the blog ids are unique, you could store them in the defaults as an array of strings:
UserDefaults.standard.set(followedBlogIds, forKey: "followedBlogIds")
You can then grab them anytime, just make sure to unwrap/cast them appropriately:
if let storedBlogIds = UserDefaults.standard.object(forKey: "followedBlogIds") as? [String] {
//filter your blogs by id and put them in the appropriate
//arrays for your table
}
When I go into the view with the collectionView my first cell have the same data of the second cell, but if i go back to the homeView and return into the collectionView it have the right data inside
I have this code for load posts from Firebase
func loadPosts(){
Toast(text:"User profile loaded").show()
let currentUser = Auth.auth().currentUser
let displayName = currentUser?.displayName
dbRef.queryOrdered(byChild: "username").queryEqual(toValue: displayName).observe(DataEventType.value, with: { (snapshot) in
var newUtente = [MyStruct]()
for snapshotValue in snapshot.children.allObjects {
let restObject = MyStruct(snapshot: snapshotValue as! DataSnapshot)
newUtente.append(restObject)
}
self.Utente = newUtente
self.Utente = self.Utente.sorted(by: { $0.trimtime?.compare($1.trimtime! as String) == .orderedDescending })
//print(self.Utente)
DispatchQueue.main.async(execute: {
self.collectionView.reloadData()
})
self.activityIndicatorView.stopAnimating()
})
}
In the code of the Cell i have this
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cella = collectionView.dequeueReusableCell(
withReuseIdentifier: "cella", for: indexPath) as! ChatViewCell
if (indexPath.row == 0){
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
cella.layer.backgroundColor = UIColor("#DAE3FB").cgColor
let user = user2[indexPath.row]
cella.Label1?.text = user.username
key = user.key
last = user.lastname
} else if (indexPath.row >= 1){
let user = Utente[indexPath.row]
cella.LabelEur.text = user.price+"€"
test19 = user.price
cella.LabelR.text = user.sender
cella.layer.backgroundColor = UIColor("#FFFFFF").cgColor
if (user.positionDef != nil) {
if (user.positionDef.hasPrefix("0")) {
cella.Distanza.text = "Nelle vicinanze"
} else {
cella.Distanza.text = user.positionDef+"KM"
}
}
cella.City?.text = user.city
self.Utente = self.Utente.sorted{ $0.price.compare($1.price, options: .numeric) == ComparisonResult.orderedAscending }
}
return cella
}
I have no idea why it happens.
After the back and the return between homeView and CollectionView
Anyone have some idea?