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