2011-10-07 121 views
0

在我的应用程序中,我必须在tableview单元格中实现复选框功能。通过单击该复选框,将在同一个单元格中创建另一个标签.1如何实现复选框功能?定制单元格。这是我尝试的代码,但它不起作用。如何将自定义图像放在表格视图单元格

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 

     UIButton *btnUncheck=[[UIButton alloc] initWithFrame:CGRectMake(260, 35, 20, 20)]; 

    btnUncheck=[UIButton buttonWithType:UIButtonTypeCustom]; 
    btnUncheck.tag=indexPath.row; 
// [btnUncheck setImage:[UIImage imageNamed:@"NO.png"] forState:UIControlStateNormal]; 
[btnUncheck addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside]; 

[view addSubview:btnUncheck] 



    -(void)checkBoxClicked:(id)sender{ 


    if(favoriteChecked==NO) 
{ 


    [sender setImage:[UIImage imageNamed:@"YES.png"] forState:UIControlStateNormal]; 
    favoriteChecked=YES; 
     } 
else 
{ 


    [sender setImage:[UIImage imageNamed:@"NO.png"] forState:UIControlStateNormal]; 
    favoriteChecked=NO; 
     } 





} 
+0

请以正确的顺序放置代码,因为我无法理解你放在'tableView:cellForRowAtIndexPath:'方法下的内容以及它完成的位置。请发布完整的代码。这没有帮助。 –

回答

1

使用UIButton *btn = (UIButton *)sender;从ID类型投下你发送按钮类别,然后更改图像btn

也改变[view addSubview:btnUncheck][cell.contentView addSubview:btnUncheck];而且这样做,你将需要创建一个TableViewcell

希望这有助于。

+0

我很高兴它帮助你:] –

1
在块

if(cell==nil) 
{ 
    //add the code here 
} 

希望这有助于

1
try this 
if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
     button.frame = CGRectMake(3,8,30, 30); 
[button setImage:[UIImage imageNamed:@"chack box1_selected_callback.png"] forState:0] 
[button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:button]; 
    } 
相关问题