2012-10-29 121 views
0

我是新來的Objective-C,並在一點..我添加UILabel(Name),在UITableViewCell,當我點擊它UIButton它顯示了一個alertView有兩個按鈕是和否。當我去是的我必須得到該單元的相應值UIlabel(Name),但我不明白該怎麼做? 這是代碼我寫創建UILabelUIButton ..iPhone中的UITableViewCell中的UIbutton?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"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. 
    } 

回答

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在

// NSLog(@"Name selected is ",[arr objectAtIndex:selectedRow] objectForKey:@"Name"]); 
+0

非常感謝.u讓我度過了一天 – Honey

0

只需創建在您的類的變量是可讀的任何地方。它可能是一個NSString或甚至一個NSDictionary與更多信息。在創建警報時進行設置。在退出警報時將其銷燬(將其設置爲nil)。

要到的信息在你的按鈕例行:

UIButton *button = (UIButton*)sender; 
UIView *cellContentView = button.superview.superview; 
UILabel *label = (UILabel*) [cellContentView viewWithTag:yourPredefinedTag]; 
_privateVariable = label.text; 
+0

最後的方法我couldnot得到U I。什麼需要的是,當我點擊一個細胞,我會用alertView。當呈現我一下就上的UIButton是的,那麼我應該得到那個特定的單元格標籤值,在那裏我單擊該單元格的UI按鈕..我該怎麼做? – Honey

+0

點擊按鈕後,您可以進入單元格,就像在編輯中我將添加到答案中。 – Mundi

+0

請提供一些代碼,你已經編輯.. – Honey

相關問題