0

我是 Objective-C 的新手,在某个点上被击中 ..我正在添加一个UILabel(Name), UIbutton(Cancel)in UITableViewCell,当我点击它时,它 UIButton会显示一个带有两个按钮 Yes 和 No 的 alertView 。当我选择 Yes 时,我必须得到相应的UIlabel(Name)该单元格的值。但我不明白该怎么做?这是我为创建一个UILabelUIButton..

-(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. 
     }
4

2 回答 2

1

您正在创建的按钮应该有一个变量指向它们所在的单元格。

    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];

应该

    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; 

并在 btnClicked

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

并在 alertView 的最后一个方法 clickedAlert at

   // NSLog(@"Name selected is ",[arr objectAtIndex:selectedRow] objectForKey:@"Name"]);
于 2012-10-29T20:58:32.673 回答
0

只需在您的类中创建一个可从任何地方读取的变量。它可能是一个NSString甚至是一个NSDictionary包含更多信息的。在创建警报时设置它。nil退出警报时将其销毁(将其设置为)。

要获取按钮例程中的信息:

UIButton *button = (UIButton*)sender;
UIView *cellContentView = button.superview.superview;
UILabel *label = (UILabel*) [cellContentView viewWithTag:yourPredefinedTag];
_privateVariable = label.text;
于 2012-10-29T11:30:51.627 回答