2

我正在制作一个带有复选框的表格视图。我确实用 UIButtons 实现了复选框,我可以毫无问题地选中和取消选中它们。当我尝试制作“全选/取消全选”按钮时出现问题,这是结果代码:

-(IBAction)select:(id)sender{
    if (all==YES) {
        all=NO;
    }
    else {
        all=YES;
    }
    [tblPeticiones reloadData];
}

问题是表没有重新加载数据。任何的想法?谢谢并恭祝安康。

编辑:我像这样加载数据

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  //Datos
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];

    NSString *fInicio = [dateFormatter stringFromDate:[(MAP_Gastos_CiberTRIPS *)[m objectAtIndex:indexPath.row] DEP_DATE]];
    NSString *loc = [(MAP_Gastos_CiberTRIPS *)[m objectAtIndex:indexPath.row] LOCATION];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [formatter setCurrencyCode:@"EUR"];
    [formatter setLocale:[NSLocale currentLocale]];
    NSString *precio = [formatter stringFromNumber:aux];
//Vista
    NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];

    CustomTVC *cell = (CustomTVC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) {
        cell = [[[CustomTVC alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
        //CheckBox
        UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        [checkButton setFrame:CGRectMake(10, 10, 23, 23)];
        if (todos==NO) {
            [checkButton setBackgroundImage:[[UIImage imageNamed:@"checkNO.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
             checkButton.tag = 0;
        }
        else {
            [checkButton setBackgroundImage:[[UIImage imageNamed:@"checkSI.png"] str    etchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal]; 
            checkButton.tag = 1;
        } 

        [checkButton addTarget:self action:@selector(checkAction:) forControlEvents:UIControlEventTouchUpInside];

        [cell addSubview:checkButton];
        [cell.contentView addSubview:checkButton]; 
  //fecha
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, 0, 70.0,tableView.rowHeight)] autorelease];
        [cell addColumn:0];
        label.tag = 1;
        label.font = [UIFont boldSystemFontOfSize:12.0];
        label.text = fInicio;
        label.textAlignment = UITextAlignmentLeft;
        label.textColor = [UIColor blackColor];
        //label.backgroundColor = [UIColor whiteColor];
        label.autoresizingMask = UIViewAutoresizingNone | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:label]; 


        if (indexPath.row % 2 == 0){ 
            label.backgroundColor = [UIColor colorWithRed:233.0/255.0 
                                               green:233.0/255.0 
                                                blue:233.0/255.0 
                                               alpha:1.0]; 
        } else { 
            label.backgroundColor = [UIColor clearColor]; 

        }


        //localización
        label =  [[[UILabel alloc] initWithFrame:CGRectMake(115, 0, 75.0,tableView.rowHeight)] autorelease];
        [cell addColumn:180];
        label.tag = 2;
        label.font = [UIFont boldSystemFontOfSize:12.0];
        label.text = loc;
        label.textAlignment = UITextAlignmentLeft;
        label.textColor = [UIColor blackColor];
        //label.backgroundColor = [UIColor whiteColor];
        label.autoresizingMask = UIViewAutoresizingNone | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:label];

        if (indexPath.row % 2 == 0){ 
            label.backgroundColor = [UIColor colorWithRed:233.0/255.0 
                                                green:233.0/255.0 
                                                 blue:233.0/255.0 
                                                alpha:1.0]; 
        } else { 
            label.backgroundColor = [UIColor clearColor];     
        }
    }
    return cell;
}
4

4 回答 4

0

调试问题 检查tableView的数据源方法reload后是否被调用?

于 2012-12-19T12:24:27.250 回答
0

您应该将检查的行存储在数组中。然后在加载/重新加载 tableview(在 cellForRowAtIndex 中)时检查此数组,以查看是否应检查该行。

然后,如果您想全选或全选,只需将它们删除或添加到数组中并重新加载 tableview。

如果您需要帮助来实现这一点,请告诉我。

于 2012-12-19T12:28:42.353 回答
0

我发现了问题。这是工作代码:

-(IBAction)select:(id)sender{

    if (all==YES) {
        all=NO;
        [btnSelAll setBackgroundImage:[[UIImage imageNamed:@"checkSI.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal]; 

    }else {

        all=YES;
        [btnSelAll setBackgroundImage:[[UIImage imageNamed:@"checkNO.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal]; 
    }
    NSMutableArray *arrIndex = [NSMutableArray new];
    for(int i=0;i<[m count];i++) {
        [arrIndex addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }
    [self.tblPeticiones beginUpdates];
    [tblPeticiones reloadRowsAtIndexPaths:arrIndex withRowAnimation:UITableViewRowAnimationNone];
    [self.tblPeticiones endUpdates]; 
    [tblPeticiones reloadData];
}
于 2012-12-19T15:26:11.780 回答
0

关于您的代码的一些评论:

  • all按下按钮时,似乎有一个名为 的全局变量,您可以切换其值。但是,在 cellForRowAtIndexPath 中,您检查另一个名为 的全局变量的值todos。这些变量之间有什么联系吗?
  • 仅在创建新单元格时更改复选框背景图像。由于单元格被回收,您需要将此代码移到外部if (cell == nil),即之后,您正在自定义回收的单元格
  • 您应该只检查if (todos)if (!todos),不要与 YES 或 NO 之类的值进行比较。切换布尔值的最简单方法是todos = !todos:)
于 2012-12-19T15:37:11.820 回答