1

I have created a check box using the uiimageview and i have placed the checkbox into the uitableview cell like below

i want to get indexpath.row when i check the check box.

so i added the uiimageviiew inside the cell. so the didSelectRowAtIndexPath is gets called and gives me the indexpath.row.

but when the row is selected i want to show the detailed view.

now this runs me into trouble.

so can you people suggest me how to tackle my above problem.

when i check the checkbox i want to get the indexpath.row.

and when the row is selected i need to show the detailed view.

Thanks for your time and help

UPDATE 1 :

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  profileName = [appDelegate.archivedItemsList objectAtIndex:indexPath.row];

  if (cell == nil)
  {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"xc"] autorelease];

     cb = [[UIButton alloc] initWithFrame:CGRectMake(5,10, unselectedImage.size.width, unselectedImage.size.height)];

     [cb setImage:unselectedImage forState:UIControlStateNormal];
    [cb setImage:selectedImage forState:UIControlStateSelected];
    [cb addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
    [cell.contentView addSubview:cb];

}

if ( tableView == myTableView )
{
   titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 150, 35)];
   titleLabel.font = [UIFont boldSystemFontOfSize:13];
   titleLabel.textColor = [UIColor blackColor]; 
    [cell.contentView addSubview:titleLabel];
   NSString *subjectData = [profileName.archive_subject stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [titleLabel setText:[NSString stringWithFormat: @"%@ ", subjectData]];

        lblDescription = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 210, 30)];
        lblDescription.numberOfLines = 2;
        lblDescription.lineBreakMode = YES;
        lblDescription.adjustsFontSizeToFitWidth = YES;
        lblDescription.font = [UIFont systemFontOfSize:10];
        lblDescription.textColor = [UIColor grayColor]; 
        [cell.contentView addSubview:lblDescription];
        NSString *CompanyName = [profileName.archive_content stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [lblDescription setText:[NSString stringWithFormat: @"%@ ", CompanyName]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   
    }


    return cell;
}
user198725878
  • 6,266
  • 18
  • 77
  • 135

2 Answers2

2

Use a UIButton instead of UIImageView for your checkbox - this way you can add an action/method to it, where you can grab the indexPath, plus you can add different images for selected/unselected state which will eliminate all the confusing stuff happening in your code above:

So in your cellForRowAtIndexPath: method:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...// Your existing code here

    UIImage *unselectedCheckboxImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"unselectedImageName" ofType:@"imageType"]];
    UIImage *selectedCheckboxImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"selectedImageName" ofType:@"imageType"]];

    UIButton *cb = [[UIButton alloc] initWithFrame:CGRectMake(desiredX, desiredY, unselectedCheckboxImage.frame.size.width, unselectedCheckboxImage.frame.size.height)];
    [cb setImage:unselectedCheckboxImage forState:UIControlStateNormal];
    [cb setImage:selectedCheckboxImage forState:UIControlStateSelected];
    [cb addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
    [cell.contentView addSubview:cb];
    [cb release];
}

And then for your button action method:

- (IBAction)buttonAction:(id)sender
{
    if ([sender isKindOfClass:[UIButton class]])
    {
        UIButton *checkboxButton = (UIButton*)sender;
        checkboxButton.selected = !checkboxButton.selected;
        NSIndexPath *indexPath = [self.myTableView indexPathForCell:(UITableViewCell*)[[checkboxButton superview] superview]];
        // Do whatever you like here 
    }
}
Rog
  • 18,602
  • 6
  • 76
  • 97
  • @Rog : Thanks for your code as i am a newbie , can you please explain your code that how it works. – user198725878 Mar 12 '11 at 09:12
  • The first thing you do is add a button to your tableview cell, and then associate an action to it. In this case everytime the user taps the button, it will trigger the `buttonAction` method. In this method, we get the indexpath by calling the superview or your cell superview, which is the actual cell. From there it is pretty easy to get the indexpath by using the tableview method `indexPathForCell`. Makes sense? If not let me know what exactly don't you understand. Cheers,Rog – Rog Mar 12 '11 at 09:43
  • @Rog : Thanks for the reply.when i scroll the table view the checkbox's check mark is getting overwritten i mean the check mark is gone..any idea? – user198725878 Mar 14 '11 at 08:02
  • @Rog: can you pls help me out? – user198725878 Mar 14 '11 at 10:50
  • Your code for setting up the cell must be inside the if (cell == nil) statement so when the system recycles a cell it is not setting it up properly. Post your updated code for cellForRowAtIndexpath: and I'll have a look. – Rog Mar 14 '11 at 11:16
  • @Rog: Hi, i have updated the code into my question..pls look into it.thanks for your help – user198725878 Mar 16 '11 at 05:24
  • @Rog : Can you pls come for chat at objective c Room pls – user198725878 Mar 16 '11 at 05:27
  • Try `if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"xc"] autorelease]; }` <-- NOTE THE ADDED CURLY BRACKET! – Rog Mar 16 '11 at 05:28
  • And you are leaking your `UIButton *cb` – Rog Mar 16 '11 at 05:30
  • Whenever i scroll the table view, the control comes to the if ( cell == nil ) condition so the cb button is getting allocated again – user198725878 Mar 16 '11 at 05:44
0

I think your logic is causing the problem in the didSelectRowAtIndexPath: method; make sure that's right. Besides, if you just want to use check mark for the cell I think it's better if you use UITableViewCellAccessoryCheckmark. This may give you a basic idea.

Community
  • 1
  • 1
visakh7
  • 26,380
  • 8
  • 55
  • 69