2013-01-07 38 views
0

我有一個UITableViewController,其中包含一個表,其中的單元格希望添加按鈕。這是一個自定義按鈕,它將充當複選框,具有正常狀態的圖像,以及用戶選擇時的圖像。不幸的是,每當我按下它時圖像都不會改變狀態,並且出於某種原因,它也不會調用目標方法。當在iOS應用程序中的UITableViewCell中按下時無法更改按鈕圖像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    // Configure the cell... 
    TestObject *tObject = [myNSMutableArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = tObject.testTitle; 

    testButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [testButton setFrame:CGRectMake(280, 57, 25, 25)]; 
    [testButton setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateSelected]; 
    [testButton setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal]; 
    [testButton setUserInteractionEnabled:YES]; 
    [testButton addTarget:self action:@selector(clickOnCheckButton:) forControlEvents:UIControlEventTouchUpInside]; 

    [cell addSubview:testButton]; 

    return cell; 

} 

爲了記錄在案,我確實實現方法:

-(void)clickOnCheckButton:(id)sender { 

    NSLog(@"button has been pressed"); 
} 

,並取得了一定的拼寫是否正確,這就是爲什麼我不明白爲什麼該方法不是原因當每個單元格中的按鈕被按下時調用。

我的表格輸出的另一個問題是,我第一個單元格里面沒有按鈕。然而,它下面的所有其他單元格都可以。

任何人都可以看到我做錯了什麼?

在此先感謝所有回覆的人。

+0

你可能會想要研究的幾件事情:一個表格單元格對所有單元格內容的內容查看屬性,你可能想嘗試加入您的按鈕單元格contentView而不是單元格。你也可以用你的按鈕來替換單元的附件視圖。 –

+0

感謝Andrew的建議。 accessoryView幫助。我現在能夠成功地將按鈕加載到單元並調用相應的方法。你可以發佈這個答案,以便我可以選擇它嗎? – syedfa

+0

謝謝!我已經這樣做了。 –

回答

0

如果您使用單元格的accessoryView屬性,則可以將其設置爲自定義視圖,如按鈕,並且其設計用作按鈕。然後您可以選擇使用UITableViewDelegate方法– tableView:accessoryButtonTappedForRowWithIndexPath:來控制操作。

0

你是在我們的項目寫的流動代碼

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


    ObjectiveCcustTcell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 
    ObjctiveString *temp=[_arraymain objectAtIndex:indexPath.row]; 


    cell.labQutionNo.text=temp.qutNO; 
    cell.labQution.text=temp.qution; 
    cell.labAns.text=temp.ans; 
    cell.labAns2.text=temp.ans2; 
    cell.labAns3.text=temp.ans3; 

[cell.ButAns1 setBackgroundImage:[UIImage imageNamed:temp.ansD1] forState:UIControlStateNormal]; 
[cell.ButAns2 setBackgroundImage:[UIImage imageNamed:temp.ansD2] forState:UIControlStateNormal]; 
[cell.ButAns3 setBackgroundImage:[UIImage imageNamed:temp.ansD3] forState:UIControlStateNormal]; 

    if(indexPath.row==0&&t==1) 
     { 
      [cell.ButAns1 setBackgroundImage:[UIImage imageNamed:@"select"] forState:UIControlStateNormal]; 
     } 

    if(indexPath.row==1&&t==3) 
      { 

      [cell.ButAns1 setBackgroundImage:[UIImage imageNamed:@"select"] forState:UIControlStateNormal]; 
      } 



    [cell.ButAns1 addTarget:self action:@selector(MEthodBut:)forControlEvents:UIControlEventTouchUpInside]; 
    [cell.ButAns2 addTarget:self action:@selector(MEthodBut2:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.ButAns3 addTarget:self action:@selector(MEthodBut3:) forControlEvents:UIControlEventTouchUpInside]; 

    [cell.ButAns1 setTag:indexPath.row]; 
    [cell.ButAns2 setTag:indexPath.row]; 
    [cell.ButAns3 setTag:indexPath.row]; 

    return cell; 



} 


-(void)MEthodBut:(UIButton *)sender 
{ 

    UIButton *button = (UIButton *)sender; 
    int row = (int)button.tag; 





    if(row==0) 
    { 

     t=1; 

[_tablemain reload]; 

    } 
} 
相關問題