tableview单元格值

时间:2011-09-23 10:43:59

标签: iphone

我正在制作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

非常感谢。

3 个答案:

答案 0 :(得分: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**
}

答案 1 :(得分: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...
    }

答案 2 :(得分: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数组 希望这会有所帮助。