2011-09-23 64 views
0

我正在制作一个iphone应用程序。在应用程序的一个视图中,我实现了表视图。在每个表格视图单元格中,我放置了一个或两个按钮。现在我希望每当我点击按钮它给我的细胞的价值。如果有人对此有所了解,请告诉我,我该怎么做?桌面单元格值

我已经实现为显示按钮的代码是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [rangetime objectAtIndex:indexPath.row]; 
    NSString *checkstatus = [finalstatusarray objectAtIndex:indexPath.row]; 
    if ([checkstatus isEqualToString:@"YES" ]) 
    { 
     UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton1.tag=1; 
     submitbutton1.frame = CGRectMake(200, 4, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"yes.png"]; 
     [submitbutton1 setImage:btnImage1 forState:UIControlStateNormal]; 
     cell.accessoryView = submitbutton1; 
     [submitbutton1 addTarget:self action:@selector(updatestatus) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    else if ([checkstatus isEqualToString:@"NO" ]) 
    { 
     UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton2.tag = 2; 
     submitbutton2.frame = CGRectMake(200, 4, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"no.png"]; 
     [submitbutton2 setImage:btnImage1 forState:UIControlStateNormal]; 
     [submitbutton2 addTarget:self action:@selector(updatestatus) forControlEvents:UIControlEventTouchUpInside]; 
     cell.accessoryView = submitbutton2; 

    } 
    else if ([checkstatus isEqualToString:@"s" ])  
    { 
     UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton1.tag = 1; 
     submitbutton1.frame = CGRectMake(255, 5, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"no.png"]; 
     [submitbutton1 setImage:btnImage1 forState:UIControlStateNormal]; 
     [cell addSubview:submitbutton1];// = submitbutton1; 
     [submitbutton1 addTarget:self action:@selector(updatestatus) forControlEvents:UIControlEventTouchUpInside]; 

     UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton2.tag = 2; 
     submitbutton2.frame = CGRectMake(285, 5, 28, 29); 
     UIImage * btnImage2 = [UIImage imageNamed:@"yes.png"]; 
     [submitbutton2 setImage:btnImage2 forState:UIControlStateNormal]; 
     [cell addSubview:submitbutton2]; 
     [submitbutton2 addTarget:self action:@selector(updatestatus) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    return cell; 
} 

enter image description here

Thanku非常多。

回答

1

你可以使用下面的代码来完成。

我已经在UIButton的目标方法中编辑了你的答案,并添加了事件作为参数,所以你可以得到该行的indexPath。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [rangetime objectAtIndex:indexPath.row]; 
    NSString *checkstatus = [finalstatusarray objectAtIndex:indexPath.row]; 
    if ([checkstatus isEqualToString:@"YES" ]) 
    { 
     UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton1.tag=1; 
     submitbutton1.frame = CGRectMake(200, 4, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"yes.png"]; 
     [submitbutton1 setImage:btnImage1 forState:UIControlStateNormal]; 
     cell.accessoryView = submitbutton1; 
     [submitbutton1 addTarget:self action:@selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    else if ([checkstatus isEqualToString:@"NO" ]) 
    { 
     UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton2.tag = 2; 
     submitbutton2.frame = CGRectMake(200, 4, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"no.png"]; 
     [submitbutton2 setImage:btnImage1 forState:UIControlStateNormal]; 
     [submitbutton2 addTarget:self action:@selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.accessoryView = submitbutton2; 

    } 
    else if ([checkstatus isEqualToString:@"s" ])  
    { 
     UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton1.tag = 1; 
     submitbutton1.frame = CGRectMake(255, 5, 28, 29); 
     UIImage * btnImage1 = [UIImage imageNamed:@"no.png"]; 
     [submitbutton1 setImage:btnImage1 forState:UIControlStateNormal]; 
     [cell addSubview:submitbutton1];// = submitbutton1; 
     [submitbutton1 addTarget:self action:@selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside]; 

     UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     submitbutton2.tag = 2; 
     submitbutton2.frame = CGRectMake(285, 5, 28, 29); 
     UIImage * btnImage2 = [UIImage imageNamed:@"yes.png"]; 
     [submitbutton2 setImage:btnImage2 forState:UIControlStateNormal]; 
     [cell addSubview:submitbutton2]; 
     [submitbutton2 addTarget:self action:@selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside]; 
    } 
    return cell; 
} 

- (void)updatestatus:(id)sender event:(UIEvent *)event 
{ 

    NSIndexPath *indexPath = [tblCityList indexPathForRowAtPoint:[[[event 
     touchesForView:sender] anyObject] locationInView:YourTableName]]; 

    **Now You can access the row value with the help of indexPath.row** 
} 
+0

非常感谢...它工作正常.. –

+0

它是我的荣幸... – SJS

0

首先,你必须修改下面的代码...

[submitbutton1 addTarget:self action:@selector(updatestatus:) forControlEvents:UIControlEventTouchUpInside]; 

相反的,

[submitbutton1 addTarget:self action:@selector(updatestatus) forControlEvents:UIControlEventTouchUpInside]; 

而且在updatestatus写代码如下:

- (IBAction) updatestatus:(id)sender 
    { 
     UIButton *btn = (UIButton*) sender; 
     UITableViewCell *cellSelected = (UITableViewCell*) [btn superview]; 
     NSIndexPath *idxPath = [tableView indexPathForCell:cell]; 
     // Your code... 
    } 
0

有以下代码

-(void)showCellValue:(UIControl *)button{ 

UITableViewCell *cell = (UITableViewCell*)button.superview; 
NSIndexPath *indexPath =[aTableView indexPathForCell:cell]; 
NSLog(@"The cell value is %@",[tableData objectAtIndex:indexPath.row]); 

}

分配该方法showCellValue给按钮的选择器。 tableData是将数据加载到表的数组。并在你的情况下它的rangeTime数组 希望这会有所帮助。