3

I list of contacts from Web Service and display it in contacts 'sectioned' tableView as seen in the screenshot.

enter image description here

Issue is I get same tag values for checkboxes of first row for section A as well as section S. I have sorted one array and displayed in the indexed table view. How to get different tag values depending on indexPath.row irrespective of number of sections displayed. Here's what I tried

    In cellForRowAtIndexPath: 
     UIButton *checkBox;
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            checkBox = [[UIButton alloc]initWithFrame:CGRectMake(7, 8, 30, 30)];
        }
        else
        {
            checkBox = [[UIButton alloc]initWithFrame:CGRectMake(15, 13, 30, 30)];
        }

        //int cnt = 0;
        // for (int i = indexPath.section - 1; i > 0 ; i--)
        // {
        //     cnt += [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:i]] count]; //arrayOfCharachters has char 'A' to 'Z'
        // }

        //checkBox.tag = cnt + indexPath.row;

        [checkBox setImage:[UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
        [checkBox addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
        [checkBox setTag:indexPath.row];
        [cell.contentView addSubview:checkBox];
        return cell;
    }

    -(void)checkBoxClicked:(id)sender
    {

        CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableViewContact];
        NSIndexPath *indexPath = [self.tableViewContact indexPathForRowAtPoint:buttonPosition];
        UIButton *tappedButton = (UIButton*)sender;
        NSLog(@"Tag number = %d", [sender tag]);

        if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"checkBox.png"]])
        {
            [sender  setImage:[UIImage imageNamed: @"checkBoxMarked.png"] forState:UIControlStateNormal];
            if(indexPath != Nil)
            {
                NSString *finalIntId = [mutableArrayOfIds objectAtIndex:indexPath.row]; // store check box ids in mutableArrayOfIds
                NSLog(@"Tagged checked button id = %@", finalIntId);
                [arrayOfIds addObject:finalIntId];
            }
            //NSString *finalIntId = [mutableArrayOfIds objectAtIndex:tappedButton.tag];
            //NSString *finalIntId = [mutableArrayOfIds objectAtIndex:indexPath.row];
        }
        else
        {
            [sender setImage:[UIImage imageNamed:@"checkBox.png"]forState:UIControlStateNormal];
            NSLog(@"UnChecked");
            //[arrayOfIds removeObjectAtIndex:tappedButton.tag];

        }
    }

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if ([arrayOfCharacters count] == 0)
    {
        return @"";
    }

    return [NSString stringWithFormat:@"%@", [arrayOfCharacters objectAtIndex:section]];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
     NSArray *toBeReturned = [NSArray arrayWithArray:
                                 [@"A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|#"
                                  componentsSeparatedByString:@"|"]];

      return toBeReturned;

}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{


        NSInteger count = 0;

        for (NSString *character in arrayOfCharacters) {

            if ([character isEqualToString:title]) {
                return count;
            }

            count ++;
        }

        return 0;


}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

        return [arrayOfCharacters count];
        //return 1;

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


        //return [mutableArray count];
        return [[objectsForCharacters objectForKey:[arrayOfCharacters objectAtIndex:section]] count];

}
1
  • better subclass the tableview cell , there by u can get whole info of the cell including sections and rows
    – Shankar BS
    Jan 20, 2014 at 6:43

5 Answers 5

21

you are setting the same tag value for all sections which have same row.
The indexPath has two properties, {section,row}.

Lets say section A has two rows,

for row1 -> indexPath.section=0, indexPath.row=0;
for row2->  indexPath.section=0, indexPath.row=1;

Lets say section S has two rows,

for row1 -> indexPath.section=1, indexPath.row=0;
for row2->  indexPath.section=1, indexPath.row=1;

So, for row1 of section A and row1 of section S, you are setting the same tag value which is 0.There is your problem.

Try setting tag value like below.

button.tag = indexPath.section*1000 +indexPath.row;

when retrieving the section and row,

NSInteger section = (button.tag)/1000;
NSInteger row = (button.tag)%1000;
8
  • so with reference to last two lines, how do I use section and row to store the tag value in array? Jan 20, 2014 at 7:18
  • why do you need to store the tagValue in array.because you dont need to store it ,as it can be known by sender.tag.
    – santhu
    Jan 20, 2014 at 7:27
  • If you explicitly need to store it, create an array as global (NSMutableArray *mutableArray). And add as [mutableArray addObject:sender.tag].But before adding check whether its already added by using for loop and check tag value matching.
    – santhu
    Jan 20, 2014 at 7:29
  • If you observe there are check boxes on the left of each tableView cell.. If I check it, it will store that tag value in the array.. Jan 20, 2014 at 7:35
  • ok ok gotcha. you cant store NSIntegers in an arrray, you should convert it to an NSNumber which is an objective c object. use [NSNumber numberWithInt:button.tag]; hope this helps.And to later get the integer from this number ,use number.integerValue
    – santhu
    Jan 20, 2014 at 7:37
2
Try this...



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }
    UIButton *checkBox =[[UIButton alloc] initWithFrame:CGRectMake(280, 10, 50, 44)];
    checkBox.backgroundColor =[UIColor orangeColor];
    [checkBox addTarget:self action:@selector(checkBoxClicked:event:)forControlEvents:UIControlEventTouchUpInside];
    [checkBox setTag:indexPath.row];
    [cell.contentView addSubview:checkBox];

    return cell;
}
- (void)checkBoxClicked:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tv]; //here tv is TableView Object
    NSIndexPath *indexPath = [self.tv indexPathForRowAtPoint: currentTouchPosition];

    NSLog(@"value of indePath.section %d ,indexPath.row %d",indexPath.section,indexPath.row);

}
1
  • how do i store the tag value inside the array depending on indexpath.section and indexpath.row. Eg. [array addObject:[arrayOfIds objectAtIndex:(indexPath.section + indexPath.row)]]; Jan 20, 2014 at 7:51
1

This is happening because you are assigning tag to buttons INDEPENDENT of sections. Both of First Row of Sections A & S have row = 0. so Tag Assigned to their respective button is 0. You should assign them Keeping reference to your sections. i would suggest to assign accessibility hint with comma separated form containing Section,Row.

And in your method -(void)checkBoxClicked:(id)sender { //pick string from comma separated form. 1st is your section, 2nd is row. }

second option is Do what ever your doing and implement your Button method like this.

    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
     ... indexpath.section is your section , index path.row is your row.
    }

There is Third option as well.

in cellforRowAtIndexpath assign your Button a title -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath [btn setTitle:<#Your Row#> forState:UIControlStateDisabled]; [btn setTag<#Your Section#>];

so upon Receiving in your Button Method you can have both Section (Tag) and Row (Title for Disabled state).

-(void)checkBoxClicked:(id)sender { [button titleForState:UIControlStateDisabled]; // your Row

button.tag //your Section }

0

Try this!

Each section you may know the count of section right, then add count of individual row.

 [[fieldTitlesList objectAtIndex:indexPath.section - 1] count] + indexPath.row

Where fieldTitlesList is the array of your sections.

0

I added the following code which solved my issue

NSInteger rowNumber = 0;
    for(NSInteger i = 0; i < indexPath.section ; i++)
    {
        rowNumber += [self tableView:self.tableViewContact numberOfRowsInSection:i];
    }

    rowNumber += indexPath.row;

[checkBox setTag:rowNumber]; //checkBox is UIButton in cellForRowAtIndexPath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.