我有一个自定义 TableViewCell,它有一个状态应该保持不变的复选框。我的问题是如何让 tableView 与自定义单元格通信,例如,当我在用户按下按钮时存储对象时,我需要知道用户按下按钮的 TableView 行,并且我需要能够将其传递给用于 NSUserDefaults 键的 TableView 中的键。我该怎么做呢?
- (IBAction)flagLectureButtonPressed:(UIButton *)sender
{
NSLog(@"sender %@",sender.description);
UIImage* selectedButton=[UIImage imageNamed:@"checkmarkSelected.png"];
UIImage* unselectedButton=[UIImage imageNamed:@"checkmark.png"];
if (sender.currentImage == unselectedButton) {
[sender setSelected:YES];
[sender setImage:selectedButton forState:UIControlStateNormal];
[self saveflagButtonState:kButtonFlagged forButton:sender];
} else {
[sender setSelected:NO];
[sender setImage:unselectedButton forState:UIControlStateNormal];
[self saveflagButtonState:kButtonNormal forButton:sender];
}
}
- (void) saveflagButtonState:(ButtonState)state forButton: (UIButton *)sender
{
static int i = 0;
NSString *object;
if (state == kButtonFlagged) {
object = @"flaged";
} else if (state == kButtonNormal) {
object = @"normal";
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:object forKey:@"key"];
[defaults synchronize];
}
- (void) setTagLectureButton:(UIButton *)tagLectureButton
{
UIImage* selectedButton=[UIImage imageNamed:@"checkmarkSelected.png"];
UIImage* unselectedButton=[UIImage imageNamed:@"checkmark.png"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *buttonIsTaged = [defaults stringForKey:@"key"];
if (buttonIsTaged) {
[tagLectureButton setImage:selectedButton forState:UIControlStateNormal];
} else {
[tagLectureButton setImage:unselectedButton forState:UIControlStateNormal];
}
}
@end