Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didSelectRowAtIndexPath with section

I have a UITableView which I have assigned sections. When using didSelectRowAtIndex with indexPath.row I am not getting the correct value. Let's say I'm in section 2, then it starts the row index at 0 again and therefore I am getting the wrong index.

Can anyone tell me how to fix this? I realize that it is possible to get the section index by indexPath.section but I can't figure out how to use this.

bandDetailViewController.band = [self.programArray objectAtIndex:indexPath.row];

I hope you can help me. Thanks in advance :-)

EDIT:

Sample data. My cells for the table view is loaded from this plist.

<array>
    <dict>
        <key>name</key>
        <string>Alphabeat</string>
        <key>description</key>
        <string>Long description.</string>
        <key>scene</key>
        <string>Store Scene</string>
        <key>date</key>
        <date>2011-02-04T20:09:40Z</date>
        <key>hasDate</key>
        <true/>
        <key>weekDay</key>
        <string>Onsdag</string>
        <key>youtubeVideo</key>
        <string>http://www.youtube.com/watch?v=dB01PTZNpBc</string>
    </dict>
    <dict>
        <key>name</key>
        <string>Anne Linnet</string>
        <key>description</key>
        <string>Long description.</string>
        <key>scene</key>
        <string>Store Scene</string>
        <key>date</key>
        <date>2011-02-04T20:09:40Z</date>
        <key>hasDate</key>
        <true/>
        <key>weekDay</key>
        <string>Onsdag</string>
        <key>youtubeVideo</key>
        <string>http://www.youtube.com/watch?v=jWMSqS7fL9k</string>
    </dict>
</array>
like image 949
simonbs Avatar asked Dec 04 '22 23:12

simonbs


2 Answers

I just came across this issue as well. I found a simple solution using the section number as you mentioned The following code uses segues to navigate to new controlView from two different sections with two different rows. VERY simple solution!

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


if (indexPath.section == 0)
{


    switch(indexPath.row) {
        case 0:
            [self performSegueWithIdentifier:@"Mission" sender:self];
            break;
        case 1:
            //[self performSegueWithIdentifier:@"Music" sender:self];
            break;

     }
}else{

    switch(indexPath.row) {
        case 0:
            [self performSegueWithIdentifier:@"Contact" sender:self];
            break;
        case 1:
            //[self performSegueWithIdentifier:@"Home" sender:self];
            break;
    }


}

}
like image 127
DCorrigan Avatar answered Jan 06 '23 15:01

DCorrigan


The rows are not consecutive and start from scratch in each section:

section 0:
row 0
row 1
row 2
row ...

section 1:
row 0
row 1
row 2
row ...

...

The best way is to build an NSMutableArray sections with NSMutableArrays for the rows. Than you code gets very easy:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return cellSections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CellSection *cellSection = [cellSections objectAtIndex:section];
    return cellSection.cellElements.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = [NSString stringWithFormat:@"Cell-%i-%i", indexPath.section, indexPath.row];

    CellSection *cellSection = [cellSections objectAtIndex:indexPath.section];
    CellElement *cellElement = [cellSection.cellElements objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

        [cell.contentView addSubview:cellElement.contentView];

    }


    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CellSection *cellSection = [cellSections objectAtIndex:indexPath.section];
    CellElement *cellElement = [cellSection.cellElements objectAtIndex:indexPath.row];

}

EDIT:

Just an example:

@interface CellSection : NSObject {

}

@property (nonatomic, retain) NSMutableArray *cellElements;
@property (nonatomic, retain) NSString *headerString;
@property (nonatomic, retain) UIView *headerView;

@end

@interface CellElement : NSObject {

}

@property (nonatomic, retain) UIView *contentView;
@property BOOL isSelectable;
@property BOOL hasAccessoryIndicator;
@property SEL action;

@end
like image 43
Manni Avatar answered Jan 06 '23 14:01

Manni