UICollectionView ReloadData not showing content properly - ios

I am trying to filter my homeCollectionView with SegmentControl. On taping on the segment, I am filtering the content based on tag available in the dictionary. When I am performing ReloadData and switching between the segments, in the first go (when I am taping on the segments the first time), the filter is working and all data is coming, but when I tap back on the segments, part of the content in the cell, especially the LabelViews text are not showing up afterwards. Also, it's happening for random indexPath.
This is my code:
#objc func toggleHomeContent(_ notification: NSNotification) {
toggleValContType = notification.object as? String ?? "all"
if (toggleValContType == "all") {
mainArrayData = primaryArrayData
}
else if (toggleValContType == "collections") {
mainArrayData = primaryArrayData { $0["filterType"] == "Col" || $0["filterType"] == "CTA" }
}
else if (toggleValContType == "books") {
mainArrayData = primaryArrayData { $0["filterType"] == "Book" || $0["filterType"] == "CTA" }
}
homeCollectionView?.reloadData()
homeCollectionView?.layoutIfNeeded()
homeCollectionView?.collectionViewLayout.invalidateLayout()
//DispatchQueue.main.async(execute: homeCollectionView.reloadData)
}
And by arrays are declared like this:
var mainArrayData : [[String:String]] = HomeArray().mainArray
var primaryArrayData: [[String:String]] = HomeArray().mainArray
Heres the snapshot of what the issue is:
Snapshot of the issue
Thanks in advance!

Add this code in main thread :
homeCollectionView?.reloadData()
homeCollectionView?.layoutIfNeeded()
DispatchQueue.main.async {
homeCollectionView?.reloadData()
homeCollectionView?.layoutIfNeeded()}

Related

How to replace dictionary particular key value using Swift

I am implementing button click to add CollectionView Cell like a Tag UI. Here, I created TagModel class for maintaining TagId and validating each time the tag data available or not by using if items.contains(tag) == false {….} (It will avoid duplication into collection view). Now, I need to add one more validation into this, if items.contains(tag) == true need to check tagName and current selected values are same. if not same I need to replace that tag value. How to achieve this?
Tag Validation Code
func tagValidation(){
// Validate A
if let aValue = UserDefaults.standard.object(forKey: "a") as? [String:Any] {
let tag = TagModel(dict: aValue)
if items.contains(tag) == false { // how to check if true need to validate current value and already exists values are same. if same no need to replace or else need to replace value
items.append(tag)
}
}
}
First ViewController
#IBAction func saveAction(_ sender: Any) {
let tag = TagModel(tagId: 0, tagName: "test", tagStoreKey: "a")
tag.save()
self.dismiss(animated: true, completion: nil)
}
Something is not right with your equality operator: you are asking if two items are the same - and if they are the same then check if something in them is different :)
Try to search array with first method: if you find (and you will) any item matching the case continue with your work.
Something like this:
if let aValue = UserDefaults.standard.object(forKey: "a") as? [String:Any] {
let tag = TagModel(dict: aValue)
if items.contains(tag) == false {
items.append(tag)
} else if let existing = items.first(where: { $0 == tag}), existing.tagName != tag.tagName {
// replace item
let index = items.firstIndex(of: tag)!
items[index] = tag
}
}

Removing CollectionView cell class from array?

I am creating a wizard using UICollectionView with an array of CollectionViewCells:
var viewCells:[BaseCVCell] = [createEventSubjectSearch(), createEventEventForm()]
This array is dynamically added to based on a series of UISwitch's that the user controls. I can add to the array fine using the code below, however I can't seem to remove an item when a user turns the switch off.
func switchToggled(sender : UISwitch) {
if sender == createDiarySwitch {
if sender.isOn {
parentClass?.viewCells.append(createEventDeferEvent())
} else {
if let i = parentClass?.viewCells.index(where: { $0 == createEventDeferEvent() }) {
parentClass?.viewCells.remove(at: i)
}
}
}
if sender == createDeferredSwitch {
if sender.isOn {
parentClass?.viewCells.append(createEventDiariseEvent())
} else {
if let i = parentClass?.viewCells.index(where: { $0 == createEventDiariseEvent() }) {
parentClass?.viewCells.remove(at: i)
}
}
}
parentClass?.wizardCollectionView.reloadData()
}
I have tried the above code, as well as:
if let index = parentClass?.viewCells.index(of: createEventDiariseEvent()) {
parentClass?.viewCells.remove(at: index)
}
Neither approach works (no errors, the code just never returns a value). I'd like to try and avoid naming elements where possible. Is there a way to do this?
Thanks for your answers, DonMag
I've achieved the desired functionality by instanciating the two dynamic cells in the main class:
let diariseCell : createEventDiariseEvent()
and then in the loop calling as thus:
if sender == createDiarySwitch {
if sender.isOn {
parentClass?.viewCells.append((parentClass?.diariseCell)!)
} else {
if let i = parentClass?.viewCells.index(where: { $0 == parentClass?.diariseCell }) {
print("Found cell reference at index \(i)")
parentClass?.viewCells.remove(at: i)
}
}
}
Works a charm now. Amazing what another pair of eyes can pick out!

How to stop viewWillAppear from completing until a function has finished

I'm pretty new to IOS Application Development.
I'm trying to stop viewWillAppear from finishing until after my function has finished working. How do I do that?
Here's viewWillAppear:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
checkFacts()
if reset != 0 {
print("removing all bird facts")
birdFacts.removeAll()
}
}
func checkFacts() {
let date = getDate()
var x: Bool = true
var ind: Int = 0
print("count is ", birdFacts.count)
while ind < birdFacts.count {
print("accessing each bird fact in checkFacts")
let imageAsset: CKAsset = birdFacts[ind].valueForKey("birdPicture") as! CKAsset
let image = UIImage(contentsOfFile: imageAsset.fileURL.path!)
print(image)
if image == nil {
if (birdFacts[ind].valueForKey("sortingDate") != nil){
print("replacing fact")
print("accessing the sortingDate of current fact in checkFacts")
let sdate = birdFacts[ind].valueForKey("sortingDate") as! NSNumber
replaceFact(sdate, index: ind)
}
/*else {
birdFacts.removeAll()
print("removing all bird facts")
}*/
}
ind = ind + 1
print(ind)
}
self.saveFacts()
let y = checkRepeatingFacts()
if y {
print("removing all facts")
birdFacts.removeAll()
//allprevFacts(date, olddate: 0)
}
}
checkFacts references 2 others functions, but I'm not sure they're relevant here (but I will add them in if they are and I'm mistaken)
Instead of trying to alter or halt the application's actual lifecycle, why don't you try using a closure?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
checkFacts(){ Void in
if self.reset != 0 {
print("removing all bird facts")
birdFacts.removeAll()
}
}
}
func checkFacts(block: (()->Void)? = nil) {
let date = getDate()
var x: Bool = true
var ind: Int = 0
print("count is ", birdFacts.count)
while ind < birdFacts.count {
print("accessing each bird fact in checkFacts")
let imageAsset: CKAsset = birdFacts[ind].valueForKey("birdPicture") as! CKAsset
let image = UIImage(contentsOfFile: imageAsset.fileURL.path!)
print(image)
if image == nil {
if (birdFacts[ind].valueForKey("sortingDate") != nil){
print("replacing fact")
print("accessing the sortingDate of current fact in checkFacts")
let sdate = birdFacts[ind].valueForKey("sortingDate") as! NSNumber
replaceFact(sdate, index: ind)
}
/*else {
birdFacts.removeAll()
print("removing all bird facts")
}*/
}
ind = ind + 1
print(ind)
}
self.saveFacts()
let y = checkRepeatingFacts()
if y {
print("removing all facts")
birdFacts.removeAll()
//allprevFacts(date, olddate: 0)
}
// CALL CODE IN CLOSURE LAST //
if let block = block {
block()
}
}
According to Apple Documentation:
Closures are self-contained blocks of functionality that can be passed around and used in your code.
Closures can capture and store references to any constants and variables from the context in which they are defined.
So by defining checkFacts() as: func checkFacts(block: (()->Void)? = nil){...} we can optionally pass in a block of code to be executed within the checkFacts() function.
The syntax block: (()->Void)? = nil means that we can take in a block of code that will return void, but if nothing is passed in, block will simply be nil. This allows us to call the function with or without the use of a closure.
By using:
if let block = block {
block()
}
we can safely call block(). If block comes back as nil, we pass over it and pretend like nothing happened. If block is not nil, we can execute the code contained within it, and go on our way.
One way we can pass our closure code into checkFacts() is by means of a trailing closure. A trailing closure looks like this:
checkFacts(){ Void in
if self.reset != 0 {
print("removing all bird facts")
birdFacts.removeAll()
}
}
Edit: Added syntax explanation.
So based on the comments, checkFacts is calling asynchronous iCloud operations that if they are not complete will result in null data that your view cannot manage.
Holding up viewWillAppear is not the way to manage this - that will just result in a user interface delay that will irritate your users.
Firstly, your view should be able to manage null data without crashing. Even when you solve this problem there may be other occasions when the data becomes bad and users hate crashes. So I recommend you fix that.
To fix the original problem: allow the view to load with unchecked data. Then trigger the checkData process and when it completes post an NSNotification. Make your view watch for that notification and redraw its contents when it occurs. Optionally, if you don't want your users to interact with unchecked data: disable appropriate controls and display an activity indicator until the notification occurs.

Swift: hot to detect editing by user has stopped in a UITextView

I have a table view where two cells have UITextView into which the user can enter long data. Following some answers here in Stackoverflow, I implemented a protocol/delegate to detect when the user has finished entering the data which then will be saved in a global dictionary:
class DetailsNewTaskViewController: UITableViewController, TextViewCellDelegate {
var cellData:[String:String] = [:]
func controllerView(controller: UITableViewCell, textViewDidEndEditing: String, atIndex:Int) {
switch(atIndex) {
case 0:
self.cellData["titolo"] = (controller as! LittleTextCell).textView.text
break
case 1:
self.cellData["oggetto"] = (controller as! BigTextCell).textView.text
break
default:
break
}
}
and this is the relative custom cell class:
class LittleTextCell: UITableViewCell, UITextViewDelegate {
#IBOutlet weak var label : UILabel!
#IBOutlet weak var textView : UITextView!
var delegate:TextViewCellDelegate?
var rowIndex:Int?
func textViewDidEndEditing(textView: UITextView) {
delegate?.controllerView(self, textViewDidEndEditing: textView.text, atIndex: rowIndex!)
}
}
where the delegate for textView is the class itself.
And this is a screenshot of the application:
The "problem" is that only AFTER the user taps another cell/field then the text is stored in the global dictionary. What about if the user taps "Fine" button (to save data) without having touched another field after he's finished entering the text? That a fatal nil error is raised. So I would like to know if there is a way to detect that the user has stopped typing in even if he's still inside that cell so that the content is always stored.
Is it possible? Is there a particular method to implement?
UPDATE: the function associated to "Fine" button:
func saveTask(sender:UIButton!) {
self.dateFormatter.dateFormat = "yyyy-MM-dd"
var taskToSave = Task(id: -1,
titolo: self.cellData["titolo"]!,
oggetto: self.cellData["oggetto"]!,
check_mail: self.cellData["check_mail"]!.toBool()!,
id_progetto: self.projects[self.cellData["progetto_nome"]!]!.id,
progetto_nome: nil,
assegnato_a: nil,
id_assegnato_a: self.users[self.cellData["assegnato_a"]!]!.id,
richiesto_da: nil,
id_richiesto_da: self.users[self.cellData["richiesto_da"]!]!.id,
priorita: self.cellData["priorita"]!,
termine_consegna: self.dateFormatter.dateFromString(self.cellData["termine_consegna"]!)!,
stato: self.cellData["stato"]!)
self.taskService.addTaskService(taskToSave) {
(response: String) in
if ((response.rangeOfString("Could not connect to the server.")) != nil) {
dispatch_async(dispatch_get_main_queue()) {
self.alertView.title = "Operazione fallita!"
self.alertView.message = "Impossibile connettersi al server. \n Riprovare."
self.alertView.delegate = self
self.alertView.addButtonWithTitle("OK")
self.alertView.show()
}
println(response)
}
else {
if ((response.rangeOfString("status code: 200")) != nil) {
dispatch_async(dispatch_get_main_queue()) {
self.alertView.title = "Operazione eseguita!"
self.alertView.message = "Task creato correttamente"
self.alertView.delegate = self
self.alertView.addButtonWithTitle("OK")
self.alertView.show()
self.navigationController?.popViewControllerAnimated(true)
}
}
else {
println(response)
}
}
}
}
define global selectedIndexPath
var selectedIndexPath:Int = 0
set it to selected indexPath
func controllerView(controller: UITableViewCell, textViewDidEndEditing: String, atIndex:Int) {
selectedIndexPath = indexPath
switch(atIndex) {
case 0:
self.cellData["titolo"] = (controller as! LittleTextCell).textView.text
break
case 1:
self.cellData["oggetto"] = (controller as! BigTextCell).textView.text
break
default:
break
}
}
In saveTask function get cell with cellForRowAtIndexPath
func saveTask(sender:UIButton!) {
let cell = tableView.cellForRowAtIndexPath(selectedIndexPath)
self.cellData["titolo"] = cell.LittleTextCell.textView.text
self.cellData["oggetto"] = cell.BigTextCell.textView.text
}
just use this line on the top in the method fine in your controller
self._tableView.superview?.endEditing(true);
I was facing the same problem , my textfields in cell and i want to check all the fields in controller.When i get my data so last data is not up to date because after last data, i click on the button not on the text field. So i found the solution. I wrote this line in my method (in your case, this line should be in fine method) before getting my dictionary and after that i had updated data.
Thanks

Contacts not showing for first time in swift

When I try to load the table view with the list of contacts, it is not showing for the first time. But if I try Pull-To-Refresh or go to some view and come back to table view controller, the contacts are loading and showing perfectly. Here is the code:
func getContactNames()
{
let allContacts = ABAddressBookCopyArrayOfAllPeople(addressBookRef).takeRetainedValue() as Array
for record in allContacts {
let currentContact: ABRecordRef = record
let currentContactName = ABRecordCopyCompositeName(currentContact)?.takeRetainedValue()
as? String
if(currentContactName != nil) {
self.arrFriendsList.append(currentContactName! as String)
}
}
// For alphabatically arranging
self.arrFriendsList = self.arrFriendsList.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
self.arrFilteredTableData = self.arrFriendsList
self.tblView.reloadData()
}
I am calling this function in viewDidLoad method. Any idea for solution? Thanks in advance :)

Resources