2012-09-03 77 views
4

我在UITableView的自定義單元中添加單選按鈕並調用其上的方法來更改其圖像或背景圖像。我可以設置從發件人選擇按鈕圖像,但我想讓其他按鈕到我原來的位置,我無法設置它,因爲下面的功能需要看起來像一個單元格中的適當的無線電按鈕在TableView的自定義單元格中添加單選按鈕並且無法訪問以外

我也面臨在這個方法中獲取適當的索引路徑的問題 - (void)TableRadioButtonSelection:(id)sender;

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

static NSString *RadioIdentifier = @"RadioCellIdentifier"; 

     RadioBtnCell *myRadiocell = (RadioBtnCell *)[tableView dequeueReusableCellWithIdentifier:RadioIdentifier]; 
     if(myRadiocell == nil) { 
      [[NSBundle mainBundle] loadNibNamed:@"RadioBtnCell" owner:self options:nil]; 
      myRadiocell = radioCell; 

      //[myRadiocell.radioBtn1 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal]; 
      myRadiocell.radioBtn1.tag = ButtonTag; 
      //[myRadiocell.radioBtn2 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal]; 
      myRadiocell.radioBtn2.tag = ButtonTag1; 
      //[myRadiocell.radioBtn3 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal]; 
      myRadiocell.radioBtn3.tag = ButtonTag2; 
      myRadiocell.tag = RadioTag; 

      [myRadiocell.radioBtn1 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside]; 
      [myRadiocell.radioBtn2 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside]; 
      [myRadiocell.radioBtn3 addTarget:self action:@selector(TableRadioButtonSelection:) forControlEvents:UIControlEventTouchUpInside]; 

      myRadiocell.backgroundView.backgroundColor = [UIColor clearColor]; 
      self.radioCell = nil; 

     } 

return myRadiocell; 

} 

- (void) TableRadioButtonSelection:(id)sender { 

    //RadioBtnCell *buttonCell = (RadioBtnCell*)[[btn superview] superview]; 

    RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag]; 
    UIButton *mybtn = (UIButton *)[cell.contentView viewWithTag:ButtonTag]; 
    UIButton *mybtn1 = (UIButton *)[cell.contentView viewWithTag:ButtonTag1]; 
    UIButton *mybtn2 = (UIButton *)[cell.contentView viewWithTag:ButtonTag2]; 

    [mybtn setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal]; 
    [mybtn1 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal]; 
    [mybtn2 setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal]; 

    //NSLog(@"%@",[cell subviews]); 
    UIView* mysubview = [cell.contentView viewWithTag:ButtonTag]; // Make sure your UITextField really *is* the last object you added. 
    UIView* mysubview1 = [cell.contentView viewWithTag:ButtonTag1]; 
    UIView* mysubview2 = [cell.contentView viewWithTag:ButtonTag2]; 

    // I am unable to set Image of other buttons apart from sender. 

    //for(UIView *item in [mysubview subviews]) { 

     if ([mysubview isKindOfClass:[UIButton class]]) { 
      UIButton *newBtn = (UIButton*)mysubview; 
      UIButton *newBtn1 = (UIButton*)mysubview1; 
      UIButton *newBtn2 = (UIButton*)mysubview2; 

      NSLog(@"OK %@",newBtn); 
      //[newBtn setBackgroundImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal]; 
      [newBtn setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal]; 
      [newBtn1 setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal]; 
      [newBtn2 setImage:[UIImage imageNamed:@"radiobtn.png"] forState:UIControlStateNormal]; 

     } 
    //} 

    NSIndexPath *indexPath = [self.questionTblView indexPathForCell:cell]; 
    NSLog(@"%d",indexPath.row); 

    // Below code is setting Image of selected Button properly as checked but above code for making button onto original state is not setting button image or background image 

    UIButton *btn = (UIButton *)sender; 
    [btn setImage:[UIImage imageNamed:@"radiobtn_chk.png"] forState:UIControlStateNormal]; 
    //[btn setBackgroundImage:[UIImage imageNamed:@"radiobtn_chk.png"] forState:UIControlStateNormal]; 
} 

回答

0

嘗試改變這一行來此; -

RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView viewWithTag:RadioTag]; 

to 

    RadioBtnCell *cell = (RadioBtnCell *)[self.questionTblView cellForRowAtIndexPath:*indexPathofthecell*]; 

通過這種方式,您可以訪問表視圖cell.After,你可以從細胞內容視圖按鈕,並做進一步的編碼。

假設如果你不知道indexPath,那麼你可以在表視圖的rowNumber中創建indexPath(我認爲你已經在RadioTag中存儲了錶行號,如果是的話,你可以使用這個字段作爲參數) 。

+0

謝謝你的答案 –

相關問題