我在索引为 0 的 UITableViewCell 上有四个按钮。我为 0、1、2、3 之类的按钮设置了标签。当我在 0 标签的按钮上将 selected 属性设置为 NO 时,它显示以下错误:
-[UITableView set Selected:]: unrecognized selector sent to instance
但是其他按钮没有错误:
代码在这里:
UIButton *button1 = (UIButton *)[tableView viewWithTag:0];
[button1 setSelected:NO];
UIButton *button2 = (UIButton *)[tableView viewWithTag:2];
[button2 setSelected:NO];
UIButton *button3 = (UIButton *)[tableView viewWithTag:3];
[button3 setSelected:NO];
[button1 setBackgroundColor:[UIColor whiteColor]];
[button2 setBackgroundColor:[UIColor whiteColor]];
[button3 setBackgroundColor:[UIColor whiteColor]];
编辑代码:
- (IBAction)click:(id)sender
{
UIButton *theButton = (UIButton *)sender;
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
NSIndexPath *clickedButtonPath = [tableView indexPathForCell:clickedCell];
NSLog(@"hi...%d",clickedButtonPath.row);
if(theButton.tag==0)
{
theButton.selected=YES;
[theButton setBackgroundColor:[UIColor redColor]];
UIButton *button1 = (UIButton *)[clickedCell viewWithTag:1];
[button1 setSelected:NO];
UIButton *button2 = (UIButton *)[clickedCell viewWithTag:2];
[button2 setSelected:NO];
UIButton *button3 = (UIButton *)[clickedCell viewWithTag:3];
[button3 setSelected:NO];
[button1 setBackgroundColor:[UIColor whiteColor]];
[button2 setBackgroundColor:[UIColor whiteColor]];
[button3 setBackgroundColor:[UIColor whiteColor]];
}
else if(theButton.tag==1)
{
theButton.selected=YES;
[theButton setBackgroundColor:[UIColor redColor]];
UIButton *button1 = (UIButton *)[clickedCell viewWithTag:0];
[button1 setSelected:NO];
UIButton *button2 = (UIButton *)[clickedCell viewWithTag:2];
[button2 setSelected:NO];
UIButton *button3 = (UIButton *)[clickedCell viewWithTag:3];
[button3 setSelected:NO];
[button1 setBackgroundColor:[UIColor whiteColor]];
[button2 setBackgroundColor:[UIColor whiteColor]];
[button3 setBackgroundColor:[UIColor whiteColor]];
}
else if(theButton.tag==2)
{
theButton.selected=YES;
[theButton setBackgroundColor:[UIColor redColor]];
UIButton *button1 = (UIButton *)[clickedCell viewWithTag:0];
[button1 setSelected:NO];
UIButton *button2 = (UIButton *)[clickedCell viewWithTag:1];
[button2 setSelected:NO];
UIButton *button3 = (UIButton *)[clickedCell viewWithTag:3];
[button3 setSelected:NO];
[button1 setBackgroundColor:[UIColor whiteColor]];
[button2 setBackgroundColor:[UIColor whiteColor]];
[button3 setBackgroundColor:[UIColor whiteColor]];
}
else if(theButton.tag==3)
{
theButton.selected=YES;
[theButton setBackgroundColor:[UIColor redColor]];
UIButton *button1 = (UIButton *)[clickedCell viewWithTag:0];
[button1 setSelected:NO];
UIButton *button2 = (UIButton *)[clickedCell viewWithTag:2];
[button2 setSelected:NO];
UIButton *button3 = (UIButton *)[clickedCell viewWithTag:1];
[button3 setSelected:NO];
[button1 setBackgroundColor:[UIColor whiteColor]];
[button2 setBackgroundColor:[UIColor whiteColor]];
[button3 setBackgroundColor:[UIColor whiteColor]];
}
}
错误:
[UITableViewCellContentView setSelected():] unrecognized selector sent to instance 0x6828f20
完整代码:
- (UITableViewCell *)tableView:(UITableView *)tablView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
subviewArray=[[NSMutableArray alloc]init];
UITableViewCell *cell = [tablView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
for(UIView *eachView in [cell.contentView subviews])
[eachView removeFromSuperview];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.contentView setBackgroundColor:[[[UIColor alloc]initWithRed:187.0/255.0 green:184.0/255.0 blue:229.0/255.0 alpha:1] autorelease]];
// [cell setUserInteractionEnabled:NO];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(indexPath.row==0)
{
UILabel *lbldoorwidth=[[UILabel alloc]init];
[lbldoorwidth setText:@"Door Width"];
[lbldoorwidth setBackgroundColor:[UIColor clearColor]];
[lbldoorwidth setTextColor:[UIColor blackColor]];
[lbldoorwidth setFrame:CGRectMake(100, 0, 200, 50)];
UIView *underline=[[UIView alloc]init];
[underline setBackgroundColor:[UIColor blackColor]];
[underline setFrame:CGRectMake(100,40,88, 2)];
UIButton *option1=[UIButton buttonWithType:UIButtonTypeCustom];
[option1 setFrame:CGRectMake(8,50,20,20)];
[option1 setTag:0];
[option1 setUserInteractionEnabled:YES];
[option1 setBackgroundColor:[UIColor whiteColor]];
[option1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
// UIImage *img1 = [UIImage imageNamed:@"radio-off.png"];
//[option1 setImage:img1 forState:UIControlStateNormal];
// UIImage *img = [UIImage imageNamed:@"radio-on.png"];
//[option1 setImage:img forState:UIControlStateSelected];
UILabel *option1Label=[[UILabel alloc]init];
[option1Label setFrame:CGRectMake(38,45,30,30)];
[option1Label setText:@"30\""];
[option1Label setBackgroundColor:[UIColor clearColor]];
UIButton *option2=[UIButton buttonWithType:UIButtonTypeCustom];
[option2 setFrame:CGRectMake(78,50,20,20)];
[option2 setTag:1];
[option2 setUserInteractionEnabled:YES];
[option2 setBackgroundColor:[UIColor whiteColor]];
[option2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
UILabel *option2Label=[[UILabel alloc]init];
[option2Label setFrame:CGRectMake(108,45,30,30)];
[option2Label setText:@"32\""];
[option2Label setBackgroundColor:[UIColor clearColor]];
UIButton *option3=[UIButton buttonWithType:UIButtonTypeCustom];
[option3 setFrame:CGRectMake(148,50,20,20)];
[option3 setTag:2];
[option3 setUserInteractionEnabled:YES];
[option3 setBackgroundColor:[UIColor whiteColor]];
[option3 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
UILabel *option3Label=[[UILabel alloc]init];
[option3Label setFrame:CGRectMake(178,45,30,30)];
[option3Label setText:@"34\""];
[option3Label setBackgroundColor:[UIColor clearColor]];
UIButton *option4=[UIButton buttonWithType:UIButtonTypeCustom];
[option4 setFrame:CGRectMake(218,50,20,20)];
[option4 setTag:3];
[option4 setUserInteractionEnabled:YES];
[option4 setBackgroundColor:[UIColor whiteColor]];
[option4 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
UILabel *option4Label=[[UILabel alloc]init];
[option4Label setFrame:CGRectMake(248,45,30,30)];
[option4Label setText:@"36\""];
[option4Label setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:lbldoorwidth];
[cell.contentView addSubview:underline];
[cell.contentView addSubview:option1];
[cell.contentView addSubview:option1Label];
[cell.contentView addSubview:option2];
[cell.contentView addSubview:option2Label];
[cell.contentView addSubview:option3];
[cell.contentView addSubview:option3Label];
[cell.contentView addSubview:option4];
[cell.contentView addSubview:option4Label];
}
return cell;
}