In my application i have a table view controller that builds a table with sections from a .plist file. Every cell is unique, as it’s based on ID parameter that is stored for every item in the plist file and cannot repeat. As example of the plist:
<dict>
<key>Section One</key>
<dict>
<key>Item 0</key>
<dict>
<key>name</key>
<string>Some name one</string>
<key>picfile</key>
<string>take8.png</string>
<key>takeItemId</key>
<integer>101</integer>
</dict>
<key>Item 1</key>
<dict>
...
</dict>
</dict>
<key>Section Two</key>
<dict>
<key>Item 0</key>
<dict>
<key>name</key>
<string>Some name two</string>
<key>picfile</key>
<string>take6.png</string>
<key>takeItemId</key>
<integer>103</integer>
</dict>
<key>Item 1</key>....
So my table consists of cells containing a text label (takeItemID), a picture from file and another text label (Name).
Now, i have some event that brings an ID as a string (I receive commands from tcp socket connection). And I want my table to highlight (to change a background) in those cells that contain received ID string as the corresponding text label. Getting NSIndexPath somehow from the plist would be OK as well.
Edit: here below is my TableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *sGroup = [sequencerGroups objectAtIndex:section];
NSDictionary *tSection = [sequencer objectForKey:sGroup];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecuencerCell"];
NSString *takeItemName = [[[tSection allKeys] sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:row];
NSDictionary *takeItem = [tSection objectForKey:takeItemName];
UILabel *idLabel = (UILabel *)[cell viewWithTag:104];
idLabel.text = [NSString stringWithFormat:@"%@",[takeItem valueForKey:@"takeItemId"]];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:102];
nameLabel.text = [takeItem valueForKey:@"name"];
UIButton *piconButton = (UIButton *)[cell viewWithTag:101];
UIImage *TakeImage = [UIImage imageNamed:[takeItem valueForKey:@"picfile"]];
[piconButton setBackgroundImage:TakeImage forState:UIControlStateNormal];
UIButton *resumeButton = (UIButton *)[cell viewWithTag:103];
UIImage *resumeImage = [UIImage imageNamed:@"resume.png"];
[resumeButton setBackgroundImage:resumeImage forState:UIControlStateNormal];
return cell;
}
Edit: here is where sequencerGroups and sequencer are taken from:
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Sequencer.plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.sequencer = dict;
[dict release];
NSArray *array = [sequencer allKeys];
self.sequencerGroups = array;
Thanks a lot,
Maxim
To create an indexPath from a rowNumber you can use:
[NSIndexPath indexPathForRow:<#(NSInteger)#> inSection:<#(NSInteger)#>];This makes row->indexPath conversion easy, once you have the row & section numbers.
So your question then becomes – how do I get the row & section numbers from the takeItemId.
I think the way I would approach it (there are numerous ways you can consider however, depending on how critical the execution time of loading your plist is) would be – right after I parse the plist, I would construct an
NSDictionarywhich maps thetakeItemIdto an IndexPath.An example:
Once you have the
mapKeyToIndexPathdictionary, you can then grab the correct indexPath when you receive thetakeItemIdin your Event and pass it to[myTableDataSource tableView:myTableView highlightCellAtIndexPath:indexPath];