8

它是否能够在方法内的分段表视图中访问选定的行位置prepareForSegue

这是我的 Segue 代码:

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

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

    Tab1_DetailTableViewController *vc = [segue destinationViewController];

    // get the selected index
    NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];

}
}

我查找了几种直接访问该位置的方法,但没有找到。我想我已经监督了一些事情。有人知道方法吗?

4

2 回答 2

23

要获取选定的索引路径:

NSIndexPath *selectedRowIndexPath = [self.tableview indexPathForSelectedRow];

获取选定的行

NSUInteger selectedRow = selectedIndexPath.row;

获取所选部分

NSUInteger selectedSection = selectedIndexPath.section;

要获取选定的单元格:

UITableViewCell *selectedCell = [self.tableview cellForRowAtIndexPath:selectedRowIndexPath];
于 2011-12-30T23:33:16.400 回答
0

在 Swift 2.2 中:

将单元格的标记放入 cellForRowAtIndexPath 委托:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! taskCell
        cell.tag = indexPath.row            
        return cell
    }

然后将其捕获到 prepareForSegue trought 发件人中:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "yourSegue" {
        let vc = segue.destinationViewController as! destinationControllerClass
        vc.passedIndex = sender?.tag
    }
}
于 2016-05-10T14:06:44.507 回答