0

我有一个TableView并且我正在使用自定义 TableCell。在 TableCell 中我有一个按钮。现在在 TableView 中,我创建了一个Bool 值来检查 Check 按钮是否被选中。问题是当我滚动表格时,单元格会被重用,因此按钮会自动取消选择。

    if (checkButton.selected==NO) {
        NSLog(@"ooo");
        checkButton.selected=YES;
        checkSelected=YES;
   }
    else {
        NSLog(@"Okkkkk");
       checkButton.selected=NO;
       checkSelected=NO;
    }

我有一个想法,使用 BOOl 的自定义属性对 UIButton进行子类化。但我不知道要锻炼。谁能帮我吗?

4

3 回答 3

0
@interface CustomButton : UIButton
@property(nonatomic) BOOL isSelected;
// DO NOT FORGET TO SYNTHESIZE MY isSelected
@end

因为您使用的是自定义单元格。您可以在自定义单元格中创建一个属性 CustomButton 。

@interface YourCustomCell : UITableViewCell
@property(nonatomic, strong) CustomButton *button;
@end

在你的 cellForRowAtIndexPath 方法中

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

   YourCustomCell *cell = (YourCustomCell*)[tableView dequeueReusableCellWithIdentifier:cellType];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellType owner:nil options:nil];
        cell = (YourCustomCell*)[nib objectAtIndex:0];
    }
    //you can add a target to your button here
    cell.button addTarget:self action:@selector(changeButtonState:).....
    if(cell.button.isSelected == YES)
          NSLog(@"selected");
    else
          NSLog(@"not selected");


-(void)changeButtonState:(CustomButton*)button
{
   if(button.isSelected) button.isSelected = NO;
   else                  button.isSelected = YES;
   [yourTableView reloadData];
}
于 2012-10-30T07:26:49.293 回答
0

您可以跟踪使用数组选择的按钮。选择按钮时,只需在特定索引处设置值 1。并在取消选择时将其设置为 0。

然后在创建单元格时,您可以按如下方式签入:

if ([[arr_Check objectAtIndex:indexPath.row] isEqualToString:@"0"])
    checkButton.selected=NO;
else
    checkButton.selected=YES;
于 2012-10-30T07:22:55.720 回答
0
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%i%i",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    else
    {
        NSArray *cellSubs = cell.contentView.subviews;
        for (int i = 0 ; i < [cellSubs count] ; i++)
        {
            [[cellSubs objectAtIndex:i] removeFromSuperview];
        }
    }

    UICheckBox *checkBox = [[UICheckBox alloc] initWithFrame:CGRectMake(0, 0, 44.0, 44.0)];
    checkBox.tag = indexPath.row;
    checkBox.titleLabel.tag = indexPath.section;
    [checkBox addTarget:self action:@selector(checkBoxPressed:) forControlEvents:UIControlEventTouchUpInside];
    for (NSIndexPath *temp in selectedObjects) {
        if ([temp isEqual: indexPath]) {
            checkBox.selected = TRUE;
        }
    }
    [cell.contentView addSubview:checkBox];

    return cell;
}

-(void)checkBoxPressed:(UICheckBox*)sender
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:sender.titleLabel.tag];
    if (sender.selected) 
        [selectedObjects removeObject:indexPath];
    else
        [selectedObjects addObject:indexPath];
    sender.selected = !sender.selected;
    NSLog(@"\n\n%@",selectedObjects);
}

selectedObjects是一个全局NSArrayUICheckBox是一个子类UIButton

于 2012-10-30T07:29:40.373 回答