Storing data on Filter Selections - ios

My doubt is regarding Filter Screen.
I want to store & remove data which user selected & deselected from Filter Screen respectively. I am not sure what data type to use here.
As it appears from below image :
Suppose I have Few filter headings like "Category", "Color", "Unit",etc.
& inside of every heading i have some values like in category I have values from category 1, category 2... upto category 50.
Now when user selects anything like Category2, category3.. I can store those particular values in Array, but when user deselect any category(unchecking the checkboxes) randomly then how i will be able to remove that particular value from array because i can't get related index inside array.(means like user selected some 10 categories then i have some 10 values inside my array but thay are not matched indexwise with values from tableView index.)
Assist me on how to approach this..

Here is how i would approach the problem. Assuming i have a data source which would contain the items without applying any filter. (In practice this is not how it would be, you would probably be getting your data source via network calls) Once a user applies or removes a filter by checking or unchecking the checkbox, i would filter the data source with the active filters.
Let's say this is your item class.
class Item {
var category: Category
var color: Color
}
enum Category {
case one
case two
.
.
case none
}
// Similarly for color and other parameters
Now this would be your filter object that maintains all the filters that are active.
class FilterParameters {
var category: [Category]? = nil
var color: [Color]? = nil
}
Create an object of this class and every time a filter is checked or unchecked, filter your data source with the current active filters. (If a filter is not selected then ignore that filter and construct filter parameters based on the ones you do have).

Related

How can i delete the value of a column in powerbuilder?

I have a datawindow with 3 selection criteria. This selection criteria are 3 columns. If the user writes something in one of them, than he can't use the others. If he tries to do that a message aware the user that he can use only one criteria. The problem is that after he deletes or cuts what he wrote before and he tries to write something in another column, the message still pop-up. I think that the buffer still contain the last value. How can i reset it?
I guess you have to nullify the 'deleted' column. I would do it that way: in the 'ItemChanged', post:
if dwo.name = 'yourcolumn' and data = '' then
SetNull(ls_null)
post dw_selection.Setitem(row, dwo.name, ls_null)
end if
This, of course, needs to be adapted to suit your needs.
Check the "itemchanged" event of the datawindow with the returned values:
Return value
Set the return code to affect the outcome of the event:
0 (Default) Accept the data value
1 Reject the data value and do not allow focus to change
2 Reject the data value but allow the focus to change

Removing Specific Object In Array Based On Property?

I have an array of drink toppings and want to remove the ones that aren't relevant to the drink, this is the code I have, but I can't figure out how to remove the topping from the array if it isn't within the criteria.
I can only remove at index path and this can change if we added more toppings etc so didn't seem accurate?
for toppings in self.toppings {
if self.selectedDrink.name == "Tea" {
if toppings.limit == "C" {
self.toppings.remove(at: toppings)
}
}
}
Essentially if the user selected Tea it looks for toppings limited for coffee and then I need to remove those ones that respond to the "C" property, but I can't see how?
Thanks for the help!
You can do in-place removal with a for loop, but it would be tricky, because you would need to iterate back to avoid disturbing indexes.
A simpler approach is to filter the array, and assign it back to the toppings property, like this:
toppings = toppings.filter {$0.limit != "C"}

UITableViewController Sections

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.

Can we get index from series data in highcharts

Having a highchart, in which there are multiple lines, how can i get index of array data from which the point has been derived.
this.series.data.indexOf( this.point)
It gives correct index of the point from line. But i need the index of the array from which series data has been loaded.
To get the index of a particular series you just use
this.series.index
with 0 being the first series.
There is a bug in yourChart.series.index selector:
It works normal when you add new series with yourChart.addSeries({..}) and it updates indexes.
But after you delete some series with yourChart.series[series.index].remove() it not updates indexes, until you add one more series.
Though, if you need to delete couple series one by one, you should select particular series by using simply yourChart.series array:
for (i in yourChart.series) {
// your search condition, for example by name
if (searchByName === chart.series[i].name) {
chart.series[i].remove();
}
}
where i is real index, not series.index!
Although, you can't select corect index by property chart.series.index which you can access at yourChart.series.options.index, you can't select series using it.

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