我有一个UITableView5 UITableViewCells.每个单元格包含一个UIButton设置如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
[cell autorelelase];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 20)];
[button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:1];
[cell.contentView addSubview:button];
[button release];
}
UIButton *button = (UIButton *)[cell viewWithTag:1];
[button setTitle:@"Edit" forState:UIControlStateNormal];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:在buttonPressedAction:方法中,我如何知道按下了哪个按钮.我考虑过使用标签,但我不确定这是最好的路线.我希望能够以某种方式标记indexPath到控件上.
- (void)buttonPressedAction:(id)sender
{
UIButton *button = (UIButton …Run Code Online (Sandbox Code Playgroud) 我需要一个UIButton来维持一个按下状态.基本上,如果按钮处于正常状态,我想触摸按钮,它突出显示其标准蓝色,然后抬起我的手指后保持蓝色.我做了以下UIAction并将按钮Touch Up Inside事件连接到它.
-(IBAction) emergencyButtonPress:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我移开手指后,按钮会返回白色背景.对于测试,我添加了一个UISwitch并让它执行相同的代码:
-(IBAction) emergencySwitchClick:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,按钮会切换到突出显示的非突出显示状态,如我所料.
在Touch Up Inside事件之后是否还有另一个事件正在将状态重置为"正常"?如何保持突出显示的状态?