【问题标题】:UIbutton in UITableViewCell in iPhone?iPhone中UITableViewCell中的UIbutton?
【发布时间】:2012-10-29 11:22:05
【问题描述】:

我是 Objective-C 的新手,在某个点上被击中 ..我在 UITableViewCell 中添加了一个 UILabel(Name),UIbutton(Cancel),当我点击它时 UIButton 它显示一个带有两个按钮的 alertView是和否。当我选择是时,我必须获得该单元格的相应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. 
     }

【问题讨论】:

    标签: iphone xcode4 uitableview


    【解决方案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的final方法中clickedAlert在

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 我无法得到你。我需要的是,当我单击单元格的 UIButton 时,我将看到一个 alertView 。当我单击是时,我应该在我单击的地方获得那个特定的单元格标签值在那个单元格的 UIbutton 上。我该怎么做?
      • 点击按钮后,您可以进入单元格,就像在我的编辑中我将添加到答案中一样。
      • 但我必须将它添加到 UIAlertView 中,正如我在代码中提到的那样。然后它不允许这个 stmnt" UIButton button = (UIButton)sender;"跨度>
      • 错了。您在按钮处理程序中创建警报视图。
      • 这就是我正在做的事情。我在这里创建它 - (IBAction)btnClicked:(id)sender{UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Are you sure" message:@"你想删除 Details" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; [警报显示]; [警报发布]; }
      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      相关资源
      最近更新 更多