1

i got this uitableview which gets its source from plist file now i want to be able to click on the table cell and have it do a action like i click on first row it does A. when i click on second row it does B. and so on up until 10 features i got in my app so far i figured out how to have the app tell me what button i pushed. but from there i have no idea how to implant a feature that will do a certain action upon clicking one of the cells. i made the uitableview on a uiview

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

//Display a quick alert view to show that this cell was pressed
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cell Pressed"
                                                    message:[NSString stringWithFormat: @"You selected the cell #%i", indexPath.row]
                                                   delegate:self cancelButtonTitle:@"Great"
                                          otherButtonTitles:nil];
[alertView show];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

plist

in the first Cell let's say i have a Test Connection and in the second cell i have a status

-(void)Testconnection { bla bla connect to something }
-(void)Status { bla bla status}

and in my plist file i have a

array -> dictionary -> string called button --- Test Connection

and

array -> dictionary -> string called button --- Status

KennyVB
  • 745
  • 2
  • 9
  • 28

3 Answers3

1

Try something like:

switch (indexPath.row) {
    case 0: [self Testconnection]; break;
    case 1: [self Status]; break;
    default: break;
};

in the didSelectRowAtIndexPath

Stas Zhukovskiy
  • 799
  • 9
  • 21
0

For simply add to your alert:

[alert setTag:indexPath.row];

and add to table delegate class

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *buttonName = [[buttonArray objectAtIndex:[alertView tag]] valueForKey:@"Button"];
    //recognize which one the button
    //do some stuff
}

Also you can use numeric identificator in buttonArray and recognize button with "switch'

outfoll
  • 73
  • 1
  • 6
0

Here is how you can call the function with the name in the plist corresponding to a cell row when it is tapped.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    //Your alert view code goes here//
    
    NSArray *plist = [NSArray arrayWithContentsOfFile:@"MyPListName"];
    NSDictionary *item = plist[indexPath.row];
    NSString *buttonName = [item[@"Button"] stringByReplacingOccurrencesOfString:@" " withString:@""];
    SEL buttonSelector = NSSelectorFromString(buttonName);

    if ([self respondsToSelector:buttonSelector]) {
        [self performSelector:buttonSelector withObject:nil];
    }
}

You should move plist object out of this function and store it at the class level so it does not get created every time a cell it tapped.

Perform selector line above throws a warning that it may leak memory since it does not know in advance if selector exists, you can fix it or supress it as explained in this post:

performSelector may cause a leak because its selector is unknown

Community
  • 1
  • 1
Yas Tabasam
  • 10,517
  • 9
  • 48
  • 53