didSelectRowAtIndexPath with section

时间:2011-02-07 16:15:55

标签: iphone objective-c sdk didselectrowatindexpath

我有一个UITableView,我已经分配了部分。当使用带有indexPath.row的didSelectRowAtIndex时,我没有得到正确的值。假设我在第2部分,然后它再次将行索引设置为0,因此我得到了错误的索引。

谁能告诉我如何解决这个问题?我意识到可以通过indexPath.section获取节索引,但我无法弄清楚如何使用它。

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

我希望你能帮助我。在此先感谢: - )

修改

示例数据。表视图的我的单元格是从这个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>

7 个答案:

答案 0 :(得分:6)

我也遇到过这个问题。我找到了一个简单的解决方案,使用你提到的节号。下面的代码使用segues从两个不同的行导航到新的controlView。非常简单的解决方案!

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


}

}

答案 1 :(得分:5)

rows不是连续的,而是从每个section开始:

第0部分:
第0行 第1行 第2行 排......

第1部分:
第0行 第1行 第2行 排......

...

最好的方法是使用NSMutableArrays为行构建NSMutableArray节。比你的代码变得容易:

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

}

编辑:

只是一个例子:

@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

答案 2 :(得分:4)

我遇到了同样的问题,我已经解决了这个问题:

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

}

希望它有所帮助:-)干杯!

答案 3 :(得分:1)

如果我正确地关注您,您可以尝试

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

或类似的东西。更典型的是,您将拥有一个表示您的部分的对象数组,并且每个部分对象都有自己的数组来表示其行。

答案 4 :(得分:1)

如果你实施了

tableview cellForRowAtIndexPath

然后在didSelectRowAtIndexPath中,您可以调用此方法来检索所选的值。如果数据结构太难,则无需重新排列数据结构。这是我最终用可变长度部分做的,并且工作得很好。

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

答案 5 :(得分:0)

处理可变长度部分的常用方法(我假设这是这里的情况)是创建一个集合,表示包含嵌套集合的部分来表示行。例如,您可以拥有顶级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:以使用相同的机制,以便为每个部分返回正确的行数。

答案 6 :(得分:-4)

如果查看NSIndexPath documentation,可以看到NSIndexPath数据结构是如何布局的。

要获取您想要执行的部分编号,请执行以下操作:

[indexPath indexAtPosition:0]

要获取该部分中的行:

[indexPath indexAtPosition:1]