我有一个 TableView ,我正在使用自定义TableCell 。在TableCell中我有一个按钮。现在在TableView中我创建了一个 Bool Value 来检查Check按钮是否被选中。 问题是当我滚动表格时,单元格被重用,因此按钮会自动取消选择。
if (checkButton.selected==NO) {
NSLog(@"ooo");
checkButton.selected=YES;
checkSelected=YES;
}
else {
NSLog(@"Okkkkk");
checkButton.selected=NO;
checkSelected=NO;
}
我有一个想法使用自定义属性BOOl 来对UIButton进行子类化。但是我不知道要锻炼。任何人都可以帮助我吗?
答案 0 :(得分:0)
您可以使用阵列保持选择按钮的轨迹。只需在选择按钮时在特定索引处设置值1。取消选择时将其设置为0。
然后在创建单元格时,您可以按如下方式签入:
if ([[arr_Check objectAtIndex:indexPath.row] isEqualToString:@"0"])
checkButton.selected=NO;
else
checkButton.selected=YES;
答案 1 :(得分: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];
}
答案 2 :(得分: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
是全球NSArray
,UICheckBox
是UIButton