views:

210

answers:

2

Hi, this is my first post here and as i am very new (2 weeks in...) to iPhone (or any!) development i am a little unsure as to how much detail is needed or relevant, so please forgive me if I dont provide enough to be useful at this stage...

Anyway, on to my problem - I have set up a UITableView-based app, which has a .plist for holding the data. The .plist has a dictionary as its root, followed by a number of Arrays, each containing the data-strings that display in the table.

Everything is working fine until the point where i select a row. I have set it up so i get an alert with the results of the button press and it is here that i want to see the contents of the cell being produced. Instead of the expected string eg. "data line 1", all i get is the number of the row within the section. I have gone backwards and forwards but dont seem to be getting anywhere although i am sure it is something simple.

The code compiles fine, with no warnings or errors, and if i can just get the string of the selected cell i will be well on my way, so any help appreciated. I know that I need to do more following the 'NSUInteger row = [indexPath row]; part but thats where my mind dries up...

here is the relevant 'selection' section, if i need to post more code please let me know, and i really appreciate any help with this...(please forgive my 'alert' message, i was just happy at that point that i get SOMETHING back from the row selection!)

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSUInteger row = [indexPath row];

    NSString *message = [[NSString alloc] initWithFormat: @"You selected Cell %d from this Section, "@"which is a very good choice indeed!" @"Unfortunately I can't work out how to get the info out of the cell so it's not much use at the moment!" @"Still, this is a good chance to see how much space I have in an alert box!", row];

UIAlertView *alert = [[UIAlertView alloc]

initWithTitle:@"My God! It works..."

message:message

delegate:nil

cancelButtonTitle:@"You are awesome Karl!!"

otherButtonTitles:nil];

[alert show];

[message release]; [alert release];

}

+1  A: 

The method only passes you the index number of whatever datasource you gave it, so refer it back to the datasource you used for the UITableView cells (I expect an NSArray).

The relevant code should be

[yourItemsArray objectAtIndex:indexPath.row];

Edit:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *key = [keys objectAtIndex:indexPath.section];
    NSArray *nameSection = [names objectForKey:key];
    NSString *rowTitle = [nameSection objectAtIndex:indexPath.row];
    NSLog(@"rowTitle = %@", rowTitle);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
handfix
Thanks for the reply, but unfortunately it hasn't solved it - i think the problem is related to the fact the .plist is broken up into sections, so i first need to point out the section, then the row within it.I dont know whats worse - not knowing what i need to do, or knowing what i need to do but not knowing how to do it!! O_o
Chubsta
Sorry, I'm unable to read that code (use code tag, pastebin, whatever). If you just need the section index consider indexPath.section.
handfix
Hi, thanks for the reply, i was looking at 'section' as that made all the sense in the world - get the section detaials, get the row details - this all points to the correct string in the array, and i should be able to get the string from it...sounds simple, but ive got a mental block on it...(i have reformatted the code if you could perhaps take a look at it again for me...thanks,karl
Chubsta
Thanks so much for the help - i had actually started to mess with NSLog but my syntax wasnt right and i wasnt really getting anywhere..I also understand how the code works that you have given me so it has been a very good learning process for meCheers,KarlThis has solved the problem and gives me the information i need to move onwards and upwards
Chubsta
A: 
Chubsta