2012-12-07 54 views
1

我有一个UITableViewCell 0索引上的四个按钮。我已经为像0,1,2,3这样的按钮设置了标签。 当我设置选择的属性没有0标记的按钮,它显示了以下错误:TableView单元格0索引错误

-[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; 
} 
+0

不使用可重复使用的tableview细胞,它的不可重复使用的 – Rajneesh071

回答

2

访问您的按钮,这样

UIButton *button1 = (UIButton *)[cell viewWithTag:0]; 


UITableViewCell *clickedCell = (UITableViewCell *)sender; 
+0

我不知道该代码是否在工作不,因为它没有显示任何错误。编辑后的代码粘贴在上面。 – kittu88

+0

什么??????????? – Rajneesh071

+0

请检查问题 – kittu88

相关问题