2011-03-24 27 views
0

我正在做一個小概念複選框。其實,我把複選框圖像放在表格單元格中,但我無法理解,如何選擇特定的複選框並在表格視圖中獲取單元格的值,任何主體都可以通過解決這個問題來幫助我。提供編碼表視圖的屏幕截圖,如何選擇特定的複選框在插入在表格單元接口生成器中的複選框iphone

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"in table view cell for row at index"); 

    RequestSongSelectingCell *cell = (RequestSongSelectingCell *) [tableView dequeueReusableCellWithIdentifier:@"requestsingcell"]; 
    if (cell==nil) 
    { 

     [[NSBundle mainBundle] loadNibNamed:@"RequestSongSelectingCell" owner:self options:nil]; 
     NSLog(@"start"); 
     cell=requestingCell; 
     NSLog(@"end"); 
    } 

    NSDictionary *dict=[self.array objectAtIndex:indexPath.row]; 

    NSString *artistname=[dict objectForKey:@"artist"]; 
    NSLog(@"artistname is %@",artistname); 

    cell.artistName.text=artistname; 

    NSString *songtitle=[dict objectForKey:@"title"]; 
    NSLog(@"songtitle is %@",songtitle); 
    cell.artistTitle.text=songtitle; 

    return cell; 
} 

-(IBAction)checkButtonPressed:(id)sender 
{ 
    NSLog(@"check box button pressed"); 

    requestingCell.checkBoxButton.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"]; 

} 

enter image description here

感謝和問候,

吉里什

回答

2

你添加的RequestSongSelectingCell.xib一個按鈕文件?

當您將其添加到tableView時,您可以設置它的標籤屬性。然後你可以在buttonPressed方法中檢索標籤。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"in table view cell for row at index"); 

    RequestSongSelectingCell *cell = (RequestSongSelectingCell *) [tableView dequeueReusableCellWithIdentifier:@"requestsingcell"]; 
     if (cell==nil) 
    { 

     [[NSBundle mainBundle] loadNibNamed:@"RequestSongSelectingCell" owner:self options:nil]; 
       NSLog(@"start"); 
     cell=requestingCell; 
     NSLog(@"end"); 
    } 
    cell.btn.tag = indexPath.row; 
    // your customization 
    return cell; 
} 

-(IBAction)checkButtonPressed:(id)sender 
{ 
    UIButton * btn = (UIButton*)sender; 
    int selected_index = btn.tag; // This is your index of selected cell 

    NSLog(@"check box button pressed"); 

    requestingCell.checkBoxButton.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"]; 

} 

編輯:爲了處理選擇

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"in table view cell for row at index"); 

    RequestSongSelectingCell *cell = (RequestSongSelectingCell *) [tableView dequeueReusableCellWithIdentifier:@"requestsingcell"]; 
     if (cell==nil) 
    { 

     [[NSBundle mainBundle] loadNibNamed:@"RequestSongSelectingCell" owner:self options:nil]; 
       NSLog(@"start"); 
     cell=requestingCell; 
     NSLog(@"end"); 
    } 
//check if cell is already selected or not 
     // if not selected, tag will be positive 
     cell.btn.tag = indexPath.row; 
     // if selected, tag will be negative 
     cell.btn.tag = -indexPath.row; 
     // your customization 
     return cell; 
    } 

-(IBAction)checkButtonPressed:(id)sender 
{ 


    UIButton * btn = (UIButton*)sender; 
    int selected_index = btn.tag; // This is your index of selected cell 


    //btn is the same btn image of which you need to change/toggle 
    if(btn.tag > 0)  //its not selected 
    { 
      //set btn image as selected 
      btn.tag = -(btn.tag); 
      [btn setImage: forState:UIControlStateNormal]; 
    } 
    else if(btn.tag < 0)  //its selected 
    { 
      //set btn image as unselected 
      btn.tag = -(btn.tag); 
      [btn setImage: forState:UIControlStateNormal]; 
    } 
    NSLog(@"check box button pressed"); 

//  requestingCell.checkBoxButton.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"]; 

    } 
+0

嗨Vaibhav的感謝烏拉圭回合的響應它的工作,但我有當我當上點擊是在butoon圖像不能改變一個問題(刻度)你可以指導我這樣做。 – girish 2011-03-24 07:19:52

+0

這很棘手,我們維護標籤中單元格的索引,因此不能將其用於選定或未選定的值。您可以將標籤設置爲正面或負面。對未選中的選擇爲正值,對選中的爲負值。這可以成爲解決這個問題的方法之一。我將編輯相同的代碼。 – 2011-03-24 07:28:43

+0

嗨vaibhav感謝它的工作..可以請你轉發我的郵件ID給我。我的郵箱ID是[email protected] – girish 2011-03-24 07:53:55

相關問題