我的 iOS 6 应用程序中有 4 个分段表。这些表都有一个我在 titleForHeaderInSection 中设置的标题。我想知道如何在 didSelectRowAtIndexPath 中使用 NSLog 访问此标题。我确实看到了我在警报中单击的行的字符串值,但我也想要 tableview 部分的标题。我不知道如何得到它。
- (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;
}