DidSelectRowAtIndexPath with section

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

Can someone tell me how to fix this? I understand that you can get the index of the indexPath.section section, but I cannot figure out how to use this.

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

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

EDIT:

Sample data. My cells for table view are 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>
+3
source share
7 answers

rowsare not consecutive and start at zero in each section:

0:
0
1
2
...

1:
0
1
2
...

...

- NSMutableArray NSMutableArrays . :

- (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:

:

@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
+5

, :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selected = [_products objectAtIndex:[self getRealIndexFromIndexPath:indexPath]];
NSLog(@"%@", selected);

}

- (NSUInteger)getRealIndexFromIndexPath:(NSIndexPath*)indexPath {   
NSUInteger temp = 0;
for (int i = 0; i < indexPath.section ; i++) {
    temp += [_mySectionedTable numberOfRowsInSection:i];
}
return temp + indexPath.row;

}

, :-) !

+4

. , , . segues . !

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


}

}
+4

,

NSUInteger index = indexPath.row * (indexPath.section + 1);
bandDetailViewController.band = [self.programArray objectAtIndex:index];

- . , , , .

+1

( , ) , , . , , NSMutableArray , NSMutableArray .

plist, NSPredicate. , :

static NSArray *days;

if (days == nil)
{
    // Populate the array with days of the week in the order 
    // in which you want the table view to present the sections.
    days = [NSArray arrayWithObjects:@"Monday", @"Tuesday", nil];
}

NSString *day = [days objectAtIndex:[indexPath section]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"weekDay like %@", day];
NSArray *filteredDicts = [[self programArray] filteredArrayUsingPredicate:predicate];

, tableView:numberOfRowsInSection:, , .

0

If you have implemented

tableview cellForRowAtIndexPath

then didSelectRowAtIndexPathyou can call this method to retrieve the selected value. No need to rebuild your data structures if it is too complicated. This is what I ended up with variable length sections and it works fine.

NSString *selectedValue = [self tableView:theTableView cellForRowAtIndexPath:indexPath].textLabel.text;

0
source

If you look at the NSIndexPath documentation , you will see how the NSIndexPath data structure is laid out.

To get the section number, you want to do something like:

[indexPath indexAtPosition:0]

To get the string in this section:

[indexPath indexAtPosition:1]
-4
source

Source: https://habr.com/ru/post/1790490/


All Articles