我想在表格视图中创建一个简单的复选框。首先,我在情节提要中创建视图,每个元素在单元格中都有一个标签。然后我在 Controller.m 中编码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
UIButton *checkbox = (UIButton *)[cell viewWithTag:103];
checkbox.selected = NO;
[checkbox setBackgroundImage:[UIImage imageNamed:@"uCheckbox2px"] forState:UIControlStateNormal];
[checkbox addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventTouchUpInside];
...
}
-(void) changeState:(UIButton *)checkbox
{
checkbox.selected = !checkbox.selected;
if (checkbox.selected)
[checkbox setBackgroundImage:[UIImage imageNamed:@"cCheckbox2px"] forState:UIControlStateSelected];
else
[checkbox setBackgroundImage:[UIImage imageNamed:@"uCheckbox2px"] forState:UIControlStateNormal];
}
但是,如果我点击按钮,图像不会改变。为什么会这样,我该如何解决?感谢帮助!