0

私は客観的 C に不慣れで、ある点で打たれました..私はUILabel(Name),を追加UIbutton(Cancel)UITableViewCellUIButtonいます。UIlabel(Name)そのセルの値.しかし、私はそれを行う方法を理解できませんでした? UILabelこれは、および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. 
     }
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 に答える