是否能够在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];
}
}
我查了几个直接访问该位置的方法,但我找不到一个。我想我已经监督了一些事情。有人知道吗?
答案 0 :(得分:23)
获取选定的索引路径:
NSIndexPath *selectedRowIndexPath = [self.tableview indexPathForSelectedRow];
获取所选行
NSUInteger selectedRow = selectedIndexPath.row;
获取所选部分
NSUInteger selectedSection = selectedIndexPath.section;
获取所选单元格:
UITableViewCell *selectedCell = [self.tableview cellForRowAtIndexPath:selectedRowIndexPath];
答案 1 :(得分: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
}
}