0

I'm new to objective-C and strucked at a point ..I'm adding a UILabel(Name),UIbutton(Cancel) in UITableViewCell and when I click on it UIButton it shows an alertView with two button Yes and No .When I go for Yes I have to get the corresponding UIlabel(Name) value of that cell.But I could not understand how to do it ? This is the code Im writing for creating a UILabel and UIButton..

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier=@"Cell";
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault    reuseIdentifier:CellIdentifier]autorelease];
    }
    NSMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row];
 UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 320,15)];
    name .font=[UIFont boldSystemFontOfSize:12];        
    [name setTextAlignment:UITextAlignmentLeft];
    [name  setText:[d valueForKey:@"Name"]];
    name.tag=112;
    [cell addSubview:name];
    [name  release];    

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100.0f, 50.0f, 75.0f, 30.0f);
    [btn setTitle:@"Delete Details" forState:UIControlStateNormal];   
    [cell addSubview:btn];
    [btn addTarget:self action:@selector(btnClicked:)
                             forControlEvents:UIControlEventTouchUpInside];

    return cell;     
}
- (IBAction)btnClicked:(id)sender

{



    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Are you sure" message:@"You want to delete Details" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
                          [alert show];
                          [alert release];    

}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex   
{
    if (buttonIndex == 0) 
    {
       [alertView dismissWithClickedButtonIndex:0 animated:YES]; 

     }
    else 
    {
        //I have to get the corresponding cells UILabel value. 
     }
Honey
  • 2,840
  • 11
  • 37
  • 71

2 Answers2

1

Your buttons which you are creating should some how have a variable which points to which cell they are in.

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100.0f, 50.0f, 75.0f, 30.0f);
    [btn setTitle:@"Delete Details" forState:UIControlStateNormal];   
    [cell addSubview:btn];
    [btn addTarget:self action:@selector(btnClicked:)
                         forControlEvents:UIControlEventTouchUpInside];

should be

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    // The below line will add a state to each button which you can use to later differentiate in your method: btnClicked
    [btn setTag:indexPath.row]; 
    btn.frame = CGRectMake(100.0f, 50.0f, 75.0f, 30.0f);
    [btn setTitle:@"Delete Details" forState:UIControlStateNormal];   
    [cell addSubview:btn];
    [btn addTarget:self action:@selector(btnClicked:)
                         forControlEvents:UIControlEventTouchUpInside];



   you can set some variable which is global to the class like "selectedRow". You can declare it on the top of the class after the line @implementation. Look for a line called

   @implementation yourClassName

   //Add the below declaration. This is a global variable in your class, which you can set in one method and access in other methods
   int selectedRow; 

and in btnClicked

//Add the below line
selectedRow=[sender tag];

and in the final method of alertView clickedAlert at

   // NSLog(@"Name selected is ",[arr objectAtIndex:selectedRow] objectForKey:@"Name"]);
Srikanth
  • 1,725
  • 2
  • 10
  • 11
0

Simply create a variable in your class that is readable from anywhere. It could be a NSString or even an NSDictionary with more information. Set it when you create the alert. Destroy it (set it to nil) when you exit the alert.

To get to the info in your button routine:

UIButton *button = (UIButton*)sender;
UIView *cellContentView = button.superview.superview;
UILabel *label = (UILabel*) [cellContentView viewWithTag:yourPredefinedTag];
_privateVariable = label.text;
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I couldnot get u .What I need is when I click on UIButton of a cell I will be presented with an alertView .When I click on Yes then I should get that particular cells label value where I click on the UIbutton of that cell..How can I do it ? – Honey Oct 29 '12 at 11:36
  • When the button is clicked you can get to the cell like in my edit I will add to the answer. – Mundi Oct 29 '12 at 11:39
  • But I have to add it in UIAlertView as I have mentioned in my code.Then it is not allowing this stmnt" UIButton *button = (UIButton*)sender;" – Honey Oct 29 '12 at 11:50
  • Wrong. You create the alert view in your button handler. – Mundi Oct 29 '12 at 12:11
  • Thats what I was doing .Im creating it here - (IBAction)btnClicked:(id)sender{UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Are you sure" message:@"You want to delete Details" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; [alert show]; [alert release]; } – Honey Oct 29 '12 at 12:15
  • -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { [alertView dismissWithClickedButtonIndex:0 animated:YES]; } else { //I have to get the corresponding cells UILabel value. } – Honey Oct 29 '12 at 12:16
  • So? Just before creating it, set the string variable as shown. – Mundi Oct 29 '12 at 12:17
  • I have to get that particular cells Name.But Im geting the same Name when I click a UIbutton of any cell...How can I do it ? – Honey Oct 29 '12 at 12:33
  • That depends where you are storing this data. Look, it seems that you should take a CS class or really try to understand some basic tutorials rather than using your valuable time so inefficiently. – Mundi Oct 29 '12 at 16:25