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