2011-10-07 118 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]; 
    } 
相關問題