我有一个UITableview,我添加了UIButton作为自定义视图。当我按下按钮时,它会更改所选按钮的标题,但是当我滚动它时,它会恢复到正常状态。请参阅下面的代码......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
static NSString *Cellidentifier = @"DataTableCellId";
STCustomCell *cell = (STCustomCell *) [tableView dequeueReusableCellWithIdentifier:Cellidentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CellView" owner:self options:nil];
cell = nib[0];
cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cellButton.tag=indexPath.row;
[cellButton addTarget:self
action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[cellButton setTitle:@"Save" forState:UIControlStateNormal];
cellButton.frame = CGRectMake(265.0, 20.0, 45.0, 27.0);
cellButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
cellButton.tintColor = [UIColor lightGrayColor];
cellButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
cellButton.layer.borderWidth = 1.0f;
cellButton.layer.cornerRadius = 5;
cellButton.layer.masksToBounds = YES;
[cell.contentView addSubview:cellButton];
}
return cell;
}
-(void)buttonClicked:(UIButton*)sender
{
[sender setBackgroundColor:[UIColor lightGrayColor]];
[sender setTintColor: [UIColor whiteColor]];
[sender setTitle:@"Saved" forState:UIControlStateNormal];
[sender addTarget:self
action:@selector(saveCell:) forControlEvents:UIControlEventTouchUpInside];
sender.enabled = NO;
}
答案 0 :(得分:1)
首先,检查你的子句是否为nil,没有必要检查你的单元格是否为nil并且不实例化它。并且由于可重复使用的行为,您的按钮会进入初始状态。因此,请检查按钮的启用属性
{
NSMutableDictionary *buttonsState;
}
- (void)viewDidLoad {
[super viewDidLoad];
buttonsState = [NSMutableDictionary new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
static NSString *Cellidentifier = @"DataTableCellId";
STCustomCell *cell = (STCustomCell *) [tableView dequeueReusableCellWithIdentifier:Cellidentifier];
UIButton *cellButton = (UIButton*)[cell.contentView viewWithTag:indexPath.row];
if (cell == nil) {
cell = [[STCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cellidentifier]
}
if (cellButton == nil)
{
cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cellButton.tag=indexPath.row;
[cell.contentView addSubview:cellButton];
}
if ([buttonsState objectForKey:@(indexPath.row)] == nil)
{
[cellButton setTitle:@"Save" forState:UIControlStateNormal];
cellButton.frame = CGRectMake(265.0, 20.0, 45.0, 27.0);
cellButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
cellButton.tintColor = [UIColor lightGrayColor];
cellButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
cellButton.layer.borderWidth = 1.0f;
cellButton.layer.cornerRadius = 5;
cellButton.layer.masksToBounds = YES;
[cellButton addTarget:self
action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
}
else
{
[cellButton setBackgroundColor:[UIColor lightGrayColor]];
[cellButton setTintColor: [UIColor whiteColor]];
[cellButton setTitle:@"Saved" forState:UIControlStateNormal];
[cellButton addTarget:self
action:@selector(saveCell:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
-(void)buttonClicked:(UIButton*)sender
{
[buttonsState setObject:@YES forKey:@(sender.tag)];
[sender setBackgroundColor:[UIColor lightGrayColor]];
[sender setTintColor: [UIColor whiteColor]];
[sender setTitle:@"Saved" forState:UIControlStateNormal];
[sender addTarget:self
action:@selector(saveCell:) forControlEvents:UIControlEventTouchUpInside];
sender.enabled = NO;
}