【问题标题】:Handle button event in UITableviewcell处理 UITableviewcell 中的按钮事件
【发布时间】:2012-02-17 12:04:52
【问题描述】:

我正在尝试从数组中获取一个数字并将其存储在 UITableview 单元格中的按钮中。当我点击按钮时,我应该能够得到号码。在按钮单击事件中从发件人那里获取信息的正确方法是什么

这是我尝试过的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil )
{
    UIButton *button = [[[UIButton alloc]init]autorelease];
    CGRect frame = CGRectMake(180, 10, 33, 33);
    button.frame = frame;
    button.tag = 1001;

    UIImage *image = [[[UIImage alloc]init]autorelease];
    image = [UIImage imageNamed:@"icon.png"];
    [button setBackgroundImage:image forState:UIControlStateNormal];

    [button addTarget:self action:@selector(onIconTapped)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    [button setHidden:YES];
    cell.accessoryView = button;  

}

int myContactID = contact.contactID;
NSLog(@"My No.  %d", myContactID);
NSNumber *mySelectedNumber = [NSNumber numberWithInt:myContactID];

UIButton *myButton = (UIButton *)[cell.accessoryView viewWithTag:1001];
for(int i = 0; i< [myContactsArray count]; i++){
    if([[[myContactsArray objectAtIndex:i]valueForKey:@"RecordID"] isEqualToNumber:mySelectedNumber]){

        [myButton setHidden:NO];
        NSString *myString;
        myString = [[myContactsArray objectAtIndex:i]valueForKey:@"MYNumber"];
        [myButton setTitle:myString forState:UIControlStateNormal];
        myButton.titleLabel.font = [UIFont systemFontOfSize:0];
        NSLog(@"my number %@", myString);
        NSLog(@"Button tag %@", myButton.currentTitle);
        break;
    }
    else{
       [myButton setHidden:YES];
    }
}
}



-(void)onIconTapped:(id)sender{
}

【问题讨论】:

    标签: iphone ios events uitableview uibutton


    【解决方案1】:
    -(void)onIconTapped:(id)sender{
    
      if (sender.tag == 1001){
      NSString *title = sender.currentTitle;
    
      NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
      [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    
      NSNumber *titleNumber = [numberFormatter numberFromString:title]; 
      }
    }
    

    【讨论】:

    • 编辑:-(void)onIconTapped:(id)sender{ UIButton *button = (UIButton *)sender; if (button.tag == 1001){// 做事}
    【解决方案2】:

    此方法来自 iOS 食谱 (Matt Drance):您可以通过将按钮的原点转换为 tableView (convertPoint:toView:) 来获取单元格,然后调用

    [[self tableView] indexPathForRowAtPoint: convertedPoint];
    

    【讨论】:

      【解决方案3】:

      1.

      [button addTarget:self action:@selector(onIconTapped:) `enter code here`forControlEvents:UIControlEventTouchUpInside];
      

      2.

      myString = [[myContactsArray objectAtIndex:i]valueForKey:@"MYNumber"];
      button.tag = i;`
      

      3.

      -(void)onIconTapped:(id)sender{
      Contact *contact = [myContactsArray objectAtIndex:sender.tag];
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-12
        • 2021-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-16
        • 1970-01-01
        相关资源
        最近更新 更多