【发布时间】:2015-06-26 18:12:48
【问题描述】:
我有一个 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;
}
【问题讨论】:
标签: ios objective-c xcode uitableview uibutton