4

我有一个 UITableView 我已经分配了部分。将 didSelectRowAtIndex 与 indexPath.row 一起使用时,我没有得到正确的值。假设我在第 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>
4

7 回答 7

9

我也刚遇到这个问题。我找到了一个使用您提到的部分编号的简单解决方案以下代码使用 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;
    }


}

}
于 2013-07-12T23:58:18.090 回答
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
于 2011-02-07T16:41:13.890 回答
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;

}

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

于 2013-07-04T08:41:39.493 回答
1

如果我正确地跟随你,你可以试试

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

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

于 2011-02-07T16:38:51.663 回答
1

如果你实施

tableview cellForRowAtIndexPath

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

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

于 2011-06-30T05:49:11.707 回答
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:以使用相同的机制,以便它为每个部分返回正确的行数。

于 2011-02-07T16:34:15.893 回答
-4

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

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

[indexPath indexAtPosition:0]

要获取该部分中的行:

[indexPath indexAtPosition:1]
于 2011-02-07T16:26:27.710 回答