Reputation: 591
I have an interesting requirement for an app I am working on. I have a UITableView
with a bunch of items in it. I am trying to log which items were looked at (scrolled past). For example, if the list contains letters A-Z and the user scrolls down the UITableView
to letter T, then values A,B,C,D,E.F....T would be stored as an NSString
in NSUserDefaults
.
I would like to store the actual row text as the value in the NSString. For example, A instead of 1, B instead of 2, etc...
I have done some digging around but cant seem to find anything useful. Any ideas on how I might accomplish this?
Upvotes: 1
Views: 112
Reputation: 115
I assume you are populating the list from array containg A,B,C and etc.
Do your array would be: (A,B,C,D,E,F,G)
So, start with converting each index of array into a NSMutableDictionary such that the array becomes as follows, here visited refers to wether that row has been visited or no, by default 0 for No:
(
{
value = "A"
visited = 0
},
{
value = "B"
visited = 0
},
{
value = "C"
visited = 0
},
{
value = "D"
visited = 0
},
)
Now on cellForRowAtIndexPath do this:
NSMutableDictionary *editDict = [arrayObjects objectAtIndex:indexPath.row];
[editDict setInteger:1 forKey:@"visited"];
[arrayObjects replaceObjectAtIndex:indexPath.row withObject:editDict];
This way all the rows that have been visited will have 1 for key "visited"
then you can run a loop to save the values corresponding to visited = 1 wherever you want
Upvotes: 1
Reputation: 4272
Make a plist ( NSDictionary
) and make pairs ( key- value ) like 1 - A 2- B and so on. When you want to store it, write a method, which will search your value, for a key.
if you have dynamic data:
make an NSMutableDictionary
, and
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
in:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[dict setObject:cell.label.text forKey:indexPath.row];
}
EDIT:
You can get the visible rows with this method:
indexPathsForVisibleRows
Returns an array of index paths each identifying a visible row in the receiver.
- (NSArray *)indexPathsForVisibleRows
Return Value An array of NSIndexPath objects each representing a row index and section index that together identify a visible row in the table view. Returns nil if no rows are visible.
Upvotes: 1
Reputation: 1034
The function
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
is called every time you view a cell. You could then use a method to convert indexPath.row into an alphabetic letter and store that in NSUserDefaults
Edit - on re-reading your question I get the impression youre just talking about generic titles of the rows. Thats easy - because you have to set the title text in cellForRowAtIndexPath - so you could just pop it into defaults when you do this.
Upvotes: 2