如何在UITableView中获取所选部分

时间:2014-02-06 19:53:37

标签: objective-c xcode uitableview

我有一个HistoryTableViewController,其中2个部分连接到详细视图控制器。我希望能够在详细信息视图中显示HistoryTableViewController中的文本和字幕文本,但我想确保它检测到正确的部分。我知道某条线上我会有这个:

if (section == 0)
{
labelCellNum.text = Whatever was the main text in the selected table view cell
labelDueDate.text = Whatever was in the subtitle text in the selected table view cell
}

 else if (section == 1)
{
labelCellNum.text = Whatever was the main text in the selected table view cell
labelDueDate.text = Whatever was in the subtitle text in the selected table view cell
}

由于我在详细信息视图控制器上显示它,我不知道如何从我从中显示的TableViewController中获取该部分。我非常需要知道如何获取该部分,以及主要文本+字幕文本。

好吧也许我不够清楚。这段代码在我的DetailsViewController的ViewDidLoad中。不在我的tableviewcontroller中。

这是HistoryTableviewController代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath         *)indexPath
{
 // HistoryDetailsViewController *HDV = [self.storyboard      instantiateViewControllerWithIdentifier:@"HistoryDetails"];
   // [self.navigationController pushViewController:HDV animated:YES];
NSLog(@"index path section--%i",[indexPath section]);
[self performSegueWithIdentifier:@"Push" sender:indexPath];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([[segue identifier] isEqualToString:@"Push"]) {

    HistoryDetailsViewController *detailVC=(HistoryDetailsViewController*)segue.destinationViewController;


    detailVC.labelCellNum.text = @"Cell test";
    detailVC.labelDueDate.text = @"Date Test";
}
}

以下是我的detailsViewController的代码:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{



labelCellNum.text = @"This isnt even getting called.";

}

3 个答案:

答案 0 :(得分:2)

检查一下,简单

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"index path section--%i",[indexPath section]);
    [self performSegueWithIdentifier:@"Push" sender:indexPath];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

if ([[segue identifier] isEqualToString:@"Push"]) {

    DetailViewController *detailVC=(DetailViewController*)segue.destinationViewController;
    detailVC.title=@"value to pass";
}
}

答案 1 :(得分:1)

你去吧!

#pragma mark - UITableViewDelegate

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

    // Selected item infos
    //
    int section = indexPath.section;
    int row = indexPath.row;
    NSLog(@"section: %d / row: %d", section, row);
}

答案 2 :(得分:-1)

我假设你有一个从HistoryVC到细节VC的segue。因此,您可以将数据传递到HistoryVC中prepareForSegue方法中的详细信息VC。在下面的代码片段中,我将单元格标题传递给新VC以设置它的标题。您可以在目标VC中设置其他属性,以满足您的需求。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
           // get selected row's cell
           //
           NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
           UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

           // set desitination VC title
           //
           UIViewController *vc = [segue destinationViewController];
           vc.navigationItem.title = cell.textLabel.text;
}