2011-03-24 22 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

相关问题