H.JC
H.JC

Reputation: 5

how to get a AlertView by click a tableViewCell(Objective-c)

I want to show alert view on clicking cell

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

}

a simplest method is enough ,thanks.

Upvotes: 0

Views: 1514

Answers (1)

Devster101
Devster101

Reputation: 578

In simple terms you can show an Alert View based on the cell clicked. So

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0) { //Change 0 to the row you want 
        [self showAlertView];
    }
}

Then in a separate function

-(void)showAlertView {
     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"YOUR TITLE"
                                                                         message:@"AND A MESSAGE OF YOUR ALERT"
                                                                         preferredStyle:UIAlertControllerStyleAlert];
     //We add buttons to the alert controller by creating UIAlertActions:
     UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"YOUR BUTTON TITLE"
                                                   style:UIAlertActionStyleDefault
                                                 handler:nil]; //You can use a block here to handle a press on this button
     [alertController addAction:actionOk];
     [self presentViewController:alertController animated:YES completion:nil];
}

EDIT*********

Based on your comment

If you want to display the alert based on the cell identifier then you could use something like this inside your didSelectRowAtIndexPath method.

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

if([cell.reuseIdentifier isEqualToString:@"CELLIDENTIFIER"]){
    [self showAlertView];
}

Upvotes: 1

Related Questions