-1

I'm working on implementing an inline date picker in a table view. However, I'm struggling on a problem with 'didSelectRowAtIndexPath'.

The sections and rows of the tableView are defined using NS_ENUM, as shown below:

typedef NS_ENUM(NSInteger, SectionOne)
{
    SectionOneFirstRow = 0,
    SectionOneSecondRow,
    TotalSectionOneRows
};

typedef NS_ENUM(NSInteger, SectionTwo) {
    SectionTwoFirstRow = 0,
    SectionTwoSecondRow,
    SectionTwoThirdRow,
    SectionTwoFourthRow,
    TotalSectionTwoRows
};

typedef NS_ENUM(NSInteger, ThirdSection) {
    ThirdSectionFirstRow = 0,
    TotalThirdSectionRows
};

typedef NS_ENUM(NSInteger, Section) {
    SectionOne = 0,
    SectionTwo,
    SectionThree,
    TotalSections,
    TotalComponents,
    TotalRows
};

Then, I implement 'didSelectRowAtIndexPath' so that if 'ThirdSectionFirstRow' is selected by the user, a date picker will be displayed. I'm using NSLog to help me debug.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == ThirdSectionFirstRow && self.datePickerIsShowing == NO){
        NSLog(@"Row selected");
        [self showDatePickerCell];
    } else{
        [self hideDatePickerCell];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

However, based on my logging results when I select each row in turn, it seems that this method is called not only if 'ThirdSectionFirstRow' is selected, but also if 'FirstSectionFirstRow' and 'SecondSectionFirstRow'. Can anyone help me spot the flaw in my logic so that this method is called only if 'ThirdSectionFirstRow' is selected? Thank you!

Community
  • 1
  • 1
narner
  • 2,908
  • 3
  • 26
  • 63
  • 1
    All of your enums start at zero, so they all overlap -- they're just integers, and the names are long gone at execution time. But you can't uniquely identify a cell by row number anyway. – Hot Licks Feb 06 '15 at 20:50
  • 1
    I think you should rethink your whole approach. Using this set of enums to define your sections and rows isn't the normal way to handle a sectioned table view. – rdelmar Feb 06 '15 at 20:50
  • What way would you suggest then? – narner Feb 06 '15 at 20:53

5 Answers5

1

Assuming you're talking about actual tableview sections, you're not testing for section -- only row, so the behavior you're describing is correct (that is, any row that matches will be a successful condition of your if).

In pseudocode, you'd need to do something like:

if ((indexPath.section == thirdSection) && (indexPath.row == firstRow) && (any other required conditions)) {
    // do stuff here
}
Brad Brighton
  • 2,179
  • 1
  • 13
  • 15
1

Basically you are not comparing the section of indexPath, you are just comparing the row with ENUM change the conditions like below for ThirdSectionFirstRow,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == SectionThree && indexPath.row == 0 && self.datePickerIsShowing == NO){
        NSLog(@"Row selected");
        [self showDatePickerCell];
    } else{
        [self hideDatePickerCell];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

if you have too many conditions better to use switch case like

switch(indexPath.section)
{
  case SectionOne:
    if(indexPath.row==0)
     {
      [self showDatePickerCell];
     }
   break;
  case SectionTwo:
   break;
}
Irfan Gul
  • 1,579
  • 13
  • 23
1

Narner, the method "didSelectRowAtIndexPath" is fired everytime you tap on ANY table row, so your logic is good, because you have an IF to detect when the tapped row is the third one. On the other hand, the "row" property of "indexPath" is a NSInteger, so maybe the expression "(indexPath.row == ThirdSectionFirstRow && self.datePickerIsShowing == NO)" is not giving you the expected result, why don't you change it for "( indexPath.row == 2 )", 2 is the third row. Maybe it is not mandatory to check if picker is showing.

0

If you want to have behaviour only on the third section's first row, you should be checking the section of your indexPath as well.

if (indexPath.section == 2 && indexPath.row == 0)

Your enums are very misleading; why are you assigning TotalComponents a value of 4 and TotalRows a value of 5? I suggest migrating to an actual data structure.

Also, you've switched from SectionOne and SectionTwo to an inverted ordering as ThirdSection. Why?

Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
0

You test only the row, if you have different section you have to test also the section.

Hikosei
  • 193
  • 1
  • 11