1

iOS 6 アプリに 4 つのセクションに分割されたテーブルがあります。これらのテーブルにはそれぞれ、titleForHeaderInSection で設定したタイトルがあります。didSelectRowAtIndexPath で NSLog を使用してこのタイトルにアクセスする方法を知りたいです。アラートでクリックした行の文字列値が表示されますが、テーブルビュー セクションのタイトルも表示したいと思います。私はそれを取得する方法がわかりません。

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

NSMutableArray *sectionArray = [self.arrayOfSections objectAtIndex:indexPath.section];

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;

NSLog(@"Selected Cell: %@", cellText);

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Selected a row" message:[sectionArray objectAtIndex:indexPath.row] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}


- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

NSString *result = nil;

if ([tableView isEqual:self.myTableView] && section == 0) {

    myTableView.tableHeaderView.tag = FROZEN;
    result = @"Frozen";

} else if ([tableView isEqual:self.myTableView] && section == 1) {

    myTableView.tableHeaderView.tag = FRUIT;
    result = @"Fruit";

}
else if ([tableView isEqual:self.myTableView] && section == 2) {

    myTableView.tableHeaderView.tag = SALADS;
    result = @"Salads";

} else if ([tableView isEqual:self.myTableView] && section == 3) {

    myTableView.tableHeaderView.tag = VEGETABLES;
    result = @"Vegetables";
}

return result;
}
4

2 に答える 2

3

セクションのタイトルを次のように配列に格納します。

NSArray *sectionTitles = [NSArray arrayWithObjects:@"Frozen", @"Fruit", @"Salads", @"Vegetables", nil];

titleForHeaderInSectionメソッドを次のように変更します。

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *result = [sectionTitles objectAtIndex:section];
//....
return result;
}

次のように変更didSelectRowAtIndexPathします。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//...
NSLog(@"Header title: %@",  [sectionTitles objectAtIndex:indexPath.section]);
//...
}

別のオプションは、以下の方法を使用することですdidSelectRowAtIndexPath

NSLog(@"Header title: %@",  [self tableView:tableView titleForHeaderInSection:indexPath.section]);
于 2012-10-10T02:54:17.743 に答える
1

UITableView がこれへのアクセスを提供する必要があるようですが、何も見つかりません... これを実装する最も簡単な方法は、配列を作成することだと思います (mySectionTitles と呼び、これはプロパティです) セクションのタイトルを付けてから、didSelectRowAtIndexPath で呼び出し[self.mySectionTitles objectAtIndex:indexPath.section]て、返された文字列で必要なことを行います。

于 2012-10-10T02:42:06.147 に答える