user198725878
user198725878

Reputation: 6386

Help me to get the IndexPath.row value

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;
}

Upvotes: 1

Views: 1384

Answers (2)

Rog
Rog

Reputation: 18670

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 
    }
}

Upvotes: 2

visakh7
visakh7

Reputation: 26390

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.

Upvotes: 0

Related Questions