UITableViewController Sections - ios

I'm trying to recreate the buggy CNContactPickerViewController made by Apple so I have an array of data [CNContact] which i need to display neatly in a UITableView. It all works great until i try to add sections based on the first letter of the contacts' last names to that table. The only solution i found is to iterate through the [CNContact] array and manually group every contact into a dictionary, based on their initials resulting in a [String:[CNContact]]. Is there a better way to do this?
The end result can be viewed in the screenshot below.

This will sort your contacts by last name. Might be overkill, as you want them grouped only by the first letter of the last name, whereas this will sort using the whole name.
var contacts :[CNContact] = [CNContact]();
// fill your contacts here
contacts.sortInPlace { (contact1, contact2) -> Bool in
contact1.familyName.compare(contact2.familyName) == .OrderedAscending
}
Not sure if you need the original ordering. If you want the original array, do create copy of the old one, and do sort in place for the copy.

Related

Strange behavior in SectionedFetchResults causes sort order to randomly jump

I have an iOS app that uses core data and CloudKit. The data model is simple and it contains a date and a string element that stores the date formatted as EEEE, MMM d yyyy.
When I run a SectionedFetchRequest and display the results in a list, after I have more than two sections the sort order all the sudden "jumps" and mixes the entries under each section. If I delete the app and reinstall it, the data that comes from iCloud is correctly formatted, but when I close the app and reopen it, again the sort order gets all mixed up. This happens with three or more sections and the order mix-up is not always the same (at least that I can observe).
Here's what I mean:
Two sections
Adding another entry, creating a third section
Closing the app and reopening, order gets mixed up
I have tried to look at how the data gets sorted via FetchedResults and SectionedFetchResults, I have tried to change how the list is created, and I have gone through the debug messages from CloudKit, but I can't figure out what's going on.
This is the data model:
And the code to fetch the data and display it on the list:
#Environment(\.managedObjectContext) private var viewContext
#FetchRequest(entity: Item.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Item.date, ascending: false)])
var entries: FetchedResults<Item>
#SectionedFetchRequest<String, Item>(
sectionIdentifier: \.dateText!,
sortDescriptors: [SortDescriptor(\.date, order: .reverse)]
)
var sections: SectionedFetchResults<String, Item>
NavigationView {
List {
ForEach(sections) { section in
Section(header: Text(section.id)) {
ForEach(section) { entry in
Text(entry.text!)
}
}
}
}
}
At this point I'm out of ideas of what else can it be. Since it's coming from iCloud formatted correctly, I'm assuming the issue is during the fetch requests and the sorting happening there, but I don't know enough to understand whether I'm doing something wrong in how I fetch the data, or whether there is another bug. Any help is appreciated.
Section order has to match sort order, the weekdays are being sorted in alphabetical order which is a different order from the dates. Try yyyy-MM-dd for the dateText. Then you will need to format it again for displaying the section text and usually that would be localised.

How to add same value to a list multiple times in one go in Dart/Flutter

Let's say I have an empty list.
If I want to add a certain number of letters "g" to this list. for example 30 40 etc. the number I send may change.
Is there a way to do this in one go without using a loop?
What is it, if any?
https://api.flutter.dev/flutter/dart-core/List/fillRange.html
I need a method like fillRange.
FillRange does not work on an empty list.
If the list is empty, don't bother using it. Just generate a new list with List.filled:
final list = List.filled(30, 'g');
EDIT: For completeness, here is how to use the above with an existing list:
final list = <String>[...];
// Mutate original list
list.addAll(List.filled(30, 'g'));
// Create new list with spread syntax
final newList = [
...list,
...List.filled(30, 'g'),
];

How to get the index based on array values

I have one array like "house". Each house object has multiple parameters like id, name, images, address. And i am showing all these in table view. Now i have one parameter key name.
I have to loop in to my house array , and find out the name which is contain in my array and i have to find out the Index.
So that in my table view i need to scroll that particular house item to top.
Any solution how can i achieve that.
Eg: Array - > [[id, name, image,address], [id, name, image,address], [id, name, image,address], [id, name, image,address]]
I have one key : "name".
I have to loop in to my array and get the index, so that i can scroll to that particular index.
Thanks
You can use the functions firstIndexOf and lastIndexOf functions available in swift. You can find details on How to find index of list item in Swift?

How to check existence by array of uniq numbers in rails

I have Disclosure model that have accession_number column. The column have unique constraint.
And when there is an array of accession_numbers, how can I know if there is accession_numbers that is not used yet.
I'm currently check existence for every numbers, but I think there is better way for this behavior.
accession_numbers.select{|number| !Disclosure.where(accession_number: number).exists?}
You can query for all disclosures which have an accession_number in your array.
existing = Disclosure.where(accession_number: accession_numbers).pluck(:accession_number)
Then just remove the existing ones from your array
accession_numbers - existing
Since you already have an unique constraint at DB level,
exiting_accessions = Disclosure.pluck(:accession_number)
results in an array of existing accesssions.
accession_array - existing_accessions results in an array of unused accessions.

swift - display items in table view by category / tag?

Im building a swift app and need some help with two problems. I'm fairly new to swift and coding in general. My app saves a bunch of items into an array, and currently the tableview on the home screen displays them. but i'd like to take it a step further and would like to be able to categorize the items. for example, a shopping list. You could save "bananas", "pasta", "apples", "bread", "strawberries", and "Milk" as items into the array.
How could I associate a tag to each item? Say, tag bananas, apples, and strawberries as "fruit" and save that item to the array.
and
How could I display just items with the "fruit" tag in a tableview? Lets say the home screen is a list of all items, but you select the tag, and it then just displays items with the "fruit" tag...etc.
this is just a simple example to help illustrate my point, but I'd like to know how to work with my data like this.
If anyone has any ideas or any resources to point me in the right direction, I'd really appreciate it.
Thanks!
To associate some things with another in Swift (and in almost all programming languages) you should use Dictionaries. How to implement it in your case? Well, very simple. First, i suggest you to create enum to store you food types or whatever:
enum FoodType {
case Fruits, Vegetables
}
Now, create your food repository, where you store food (or value) and associate its with some tag (or key):
var myFood = [FoodType : [String]]()
Now you can add you food to your myFood variable like this:
food[.Fruits] = ["Bananas", "Apples", "Strawberries"]
food[.Vegetables] = ["Potato", "Carrot"]
To select fruits do this:
var onlyFruits = food[.Fruits]
Now, onlyFruits contains only food that tagged by .Fruits.

Resources