子类Uibutton以保留使用iPhone中的自定义Tablecell选定的检查



我有一个 tableView ,我正在使用自定义tablecell .in tablecell我有一个按钮。现在,在TableView中,我创建了一个 bool值检查是否选择了检查按钮。问题是当i 滚动表时,单元格重复使用,因此按钮可以自动取消选择。

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

我有一个想法子类Uibutton,并具有Bool 的自定义属性。但是我不知道锻炼。谁能帮我吗?

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

然后在创建单元格时,您可以按以下方式检查:

if ([[arr_Check objectAtIndex:indexPath.row] isEqualToString:@"0"])
    checkButton.selected=NO;
else
    checkButton.selected=YES;
-(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(@"nn%@",selectedObjects);
}

这里selectedObjects是全局NSArrayUICheckBoxUIButton

的子类
@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];
}

最新更新