如何访问Tableview Cell中的复选框?

时间:2011-05-11 06:41:46

标签: iphone

Hai,我打印每行的复选框。但是当我们选择任何复选框时,只有最后一个单元格复选框将被选中。下面一个是用于打印复选框和每日,每周,每月数据的代码。

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[UITableViewCell alloc]init];

    cell = [self getCellContentView:CellIdentifier];
    UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
    UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
    UILabel *lblTemp3 = (UILabel *)[cell viewWithTag:3];
    UILabel *lblTemp4 = (UILabel *)[cell viewWithTag:4];

    checkButton=[UIButton buttonWithType:UIButtonTypeCustom];
    checkButton.frame = CGRectMake(5,30, 15, 10); 
    [checkButton addTarget:self action:@selector(SelectCheckBox) forControlEvents:UIControlEventTouchUpInside];
    pictureimageView=[[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 100, 60)];
    //pictureimageView=[[UIImageView alloc]initWithFrame:CGRectMake(2, 3, 65, 65)];
    [pictureimageView.layer setBorderColor:[[UIColor grayColor] CGColor]];
    [pictureimageView.layer setShadowColor:[[UIColor grayColor] CGColor]];
    [pictureimageView.layer setMasksToBounds:YES];
    [pictureimageView.layer setBorderWidth:1.5];
    [pictureimageView.layer setCornerRadius:5.0];
    [pictureimageView.layer setShadowRadius:1.0];
    [pictureimageView.layer setShadowOpacity:3.0];

    if(selectedSegment==0) 
    {

        databasefields=[eventData objectAtIndex:indexPath.row];
             lblTemp1.text=databasefields.DBtitle;
        lblTemp2.text=databasefields.DBlocation;
                lblTemp3.text=databasefields.DBdate ;
        lblTemp4.text=databasefields.DBtime;
        //NSString *pictureString=[[NSString alloc]init];
        pictureString=databasefields.DBpicture;
        checkimage = [UIImage imageNamed:@"checkbox.PNG"];
        [checkButton setImage:checkimage forState:UIControlStateNormal]; 
        pictureimageView.image = [UIImage imageWithContentsOfFile:pictureString];
        [cell.contentView addSubview:checkButton];
        [cell.contentView addSubview:pictureimageView];
    }
    else 
        if(selectedSegment==1)
        {

            databasefields=[eventData objectAtIndex:indexPath.row];
            lblTemp1.text=databasefields.DBtitle;
            lblTemp2.text=databasefields.DBlocation;
            lblTemp3.text=databasefields.DBdate;
            lblTemp4.text=databasefields.DBtime;
            pictureString=[[NSString alloc]init];
            pictureString=databasefields.DBpicture;
            checkimage = [UIImage imageNamed:@"checkbox.PNG"];
            pictureimageView.image = [UIImage imageWithContentsOfFile:pictureString];
            [cell.contentView addSubview:checkButton];
            [cell.contentView addSubview:pictureimageView];

        }
        else if(selectedSegment==2)
        {                

            databasefields=[eventData objectAtIndex:indexPath.row];
            lblTemp1.text=databasefields.DBtitle;
            lblTemp2.text=databasefields.DBlocation;
            lblTemp3.text=databasefields.DBdate;
            lblTemp4.text=databasefields.DBtime;
            pictureString=[[NSString alloc]init];
            pictureString=databasefields.DBpicture;
            checkimage = [UIImage imageNamed:@"checkbox.PNG"];
            [checkButton setImage:checkimage forState:UIControlStateNormal];
            pictureimageView.image = [UIImage imageWithContentsOfFile:pictureString];
            [cell.contentView addSubview:checkButton];
            [cell.contentView addSubview:pictureimageView];

        }

    // Configure the cell...

    return cell;
}
-(void) SelectCheckBox
{

    if (checkboxSelected == 0){
        [checkButton setSelected:YES];
        checkimage = [UIImage imageNamed:@"checkbox-on.PNG"];
        [checkButton setImage:checkimage forState:UIControlStateNormal]; 
        checkboxSelected = 1;
    } else {
        [checkButton setSelected:NO];
        checkimage = [UIImage imageNamed:@"checkbox.PNG"];
        [checkButton setImage:checkimage forState:UIControlStateNormal]; 
        checkboxSelected = 0;
    }


    ; 

}

请给我选择每个复选框的解决方案

1 个答案:

答案 0 :(得分:0)

请阅读Table View Programming Guide以了解细胞。在这种情况下,看起来checkButton是表委托的实例变量,而这正是您在-SelectCheckBox方法中修改的内容。但是,该变量仅指向您最近创建的单元格中的按钮。如果要对特定单元格进行操作,则应首先获取该行的单元格,然后找到其复选框按钮。一种方法是在设置单元格时将tag属性设置为特定值。然后,您可以向单元格询问具有该标记的子视图。