I intend to have a UITableView filled with entries from an NSMutableDictionary. What I would like to do is when checking a cell, it creates an array which contains the key's objects.
The table view's cell.textLabel.text is equal to the key of each element of the dictionary, while the object associated with the key is an integer.
Let's say I tap one of the cells, and the object associated with it is 5722. I want to add that to the array, and check the cell as well. Also, tapping the cell again should remove the object associated with it and uncheck the cell. How do I go about doing this?
As of now, when scrolling, my checkmarks go everywhere.
I think that you are better of having your datasource as an NSArray instance. The list of keys provided by the allKeys method of the NSDictionary instance is not ordered and will change at any time. If you use an array, you can store the selected cells using an NSMutableIndexSet, and use this instance to get the records that have been selected from the array using the NSArray method objectsAtIndexes
Related
I have a NSMutableDictionary that I use with a UITableView, in the table each section header is the value of a key and the cells for each section is derived from the key's value (an array).
I would like the table view to have a specific key at the top and am not quite sure how to do that.
I suggest changing your data structure to an array of custom "TableSection" objects. Each object would have a header property and an array of values for the cells.
Storing the "TableSection" objects in a mutable array lets you order them whatever way you need.
A dictionary (NSDictionary or Swift Dictionary) is not suitable for storing the contents of a table view. Dictionaries are inherently unordered, and a table view must have its contents presented in a consistent order.
You can certainly use an array of dictionaries. That's pretty common. For a sectioned table view an array of arrays of dictionaries is quite common (outer array has an entry for each section, and inner array has an entry for each row in a given section.)
If you have special contents to go in the section header you can create a section object that has a header object an an array of row objects (dictionaries or custom "row" data objects), and then have an array of section objects. Then you make your data source methods serve up cells using whatever data structure you come up with.
I have a custom view that exists in a cell in a tableview. The view is called bulletRow and it is a series of bullets that can be filled in or emptied when a user taps on them. Each cell in my tableview contains some bulletRows and I need to save them when the user taps on them. I have considered using Core Data, but I don't need them to persist when the app is shut down, I only need it to exist when the user scrolls past the dequeueing point.
Here is my situation right now: The default state for bulletRows is to have 5 dots, all of them empty. When a user taps on them they become filled. If the user scrolls down however, they get reset back to being empty. How can I save the state of the bulletRows?
The bulletRows have a property called numberOfFilledCircles which can be set at anytime to change the amount of filled in circles. This is all done in Swift as well.
In general, you should use something, such as an array, to hold the state of your table. The cells in your table should reflect that state, and update that state when selected.
You might start with an array of integers in your table view controller, like this:
var numberOfFilledCircles = [Int]()
Use the number of items in your array to determine how many rows to display in your table, by returning numberOfFilledCircles.count from your numberOfRowsInSection method.
You can populate the array in viewDidLoad. If you're hardcoding the rows, you can repeat this statement for as many rows you want:
numberOfFilledCircles.append(0)
Each Int in the array holds the value representing how many circles are filled (initialize to 0).
In your cellForRowAtIndexPath, use the appropriate value from your array when constructing your cell. For example, if your cell had a UILabel called numberOfFilledCircles, you would do this:
cell.numberOfFilledCircles.text = String(numberOfFilledCircles[indexPath.row])
Finally, in your didSelectRowAtIndexPath, update the array with the number of circles you want filled in:
numberOfFilledCircles[indexPath.row] = //whatever you want
The issue here is that the UI is not the model.
When the ui elements are pressed, you should send an action to the underlying model to update its state, and when cells are dequeued you should restore the checkbox state from the appropriate model element.
I have a view with UITableView and I populate NSMutableDictionary and bind it to UITableView.
I add items like: c,b,a,d but data in table are displayed like: a,b,c,d.
Is it possible to disable ordering so items will be displayed in table in the same order as I added them in NSMutableDictionary?
If the order objects are displayed in your table view matters, you should be using an NSArray or NSMutableArray. These are the only Objective-C collections for which the order can be guaranteed, and they work a lot easier with table views than any other collections, given that their objects are stored at indexes and these can match exactly to the row portion of the NSIndexPath argument of cellForRowAtIndexPath:.
When I add objects to my datasource, I am not able to see the added objects in my collectionview.
I am able to see in the numberOfItemsForSection increment in value, but the corresponding cells do not display the objects. The indexpath.row does not exceed its original value that I set before.
HALP.
In an app I previously had help with here, I parse an XML located online, show only 1 item from the XML the first day, and add an item each day after that the app gets opened. I would like to be able to add a 'Mark As Read' action to each cell's detail view. This way, the user could read it, mark it as read, and on subsequent loads, a checkmark would appear next to each item loaded.
Since the app downloads the XML each time, here is the idea I thought may work the best to do this. I was thinking of having an array stored to a NSUserDefault Key. This array would have a number added to it based off what row they selected. If they selected row 1 and marked it, the array would add the #1 to it. If they then selected row 3 and marked it, the array would have 1 & 3 in it.
Is this something doable, and then in cellForRowAtIndexPath, have it add a checkmark to each row # that is included in the array?
I have the idea in my head, and if this works, just need a little guidance for implementing it. Thanks
UPDATE
Here is what little I do have so far. In the applicationDidFinishLaunchingWithOptions, I check if the NSUserDefault key 'checkedrows' exists. If not, I create it with an object of an empty array.
if (![defaults objectForKey:#"checkedrows"]) {
[defaults setObject:#[] forKey:#"checkedrows"];
}
The two main questions I have is how to in the cellForRowAtIndexPath get the numbers that are in the array, and if any rows match them, add a checkmark to it. Example: There are 5 rows in the tableview, and the array returns 0, 2, 4. So, I want the TableView to add a checkmark to first third and fifth cells.
The other main question is how to go about adding a number into the array, without deleting any of the old numbers.
Absolutely!
You can use the setObject:forKey: method to write an NSArray object to NSUserDefaults, and objectForKey: to retrieve that array.
Now, simply listen out for
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
in your UITableViewDelegate, retrieve the array already existing in the NSUserDefaults, add the index that was just tapped (get this info. from the indexPath object) to the array, and simply write the array back to NSUserDefaults!
To add a checkmark to selected cells, use the following command inside thetableView:didSelectRowAtIndexPath: delegate method above:
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;