Answers:
如果使用prepareForSegue:sender:
,则以后再决定从表视图外部的某些控件触发segue时,将没有太多更改。
该prepareForSegue:sender:
消息被发送到当前视图控制器,所以我建议是这样的:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Assume self.view is the table view
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
DetailObject *detail = [self detailForIndexPath:path];
[segue.destinationViewController setDetail:detail];
}
在Swift中:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let path = self.tableView.indexPathForSelectedRow()!
segue.destinationViewController.detail = self.detailForIndexPath(path)
}
self.view
应该只是在sender
这里吗?我什[self.view indexPathForSelectedRow]
至无法上班,我不得不做[sender indexPathForSelectedRow];
tableView.indexPathForSelectedRow()
时包含正确的值很奇怪prepareFroSegue...
:tableView(_:didSelectrowAtIndexPath:)
直到稍后才调用。
我做到了,它奏效了
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Row Selected = %i",indexPath.row);
[self performSegueWithIdentifier:@"testID" sender:self.view];
}
当发件人是UITableViewCell时,您可以要求UITableView查询单元格的indexPath。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let cell = sender as? UITableViewCell {
let indexPath = self.tableView.indexPathForCell(cell)!
assert(segue.destinationViewController.isKindOfClass(DetailViewController))
let detailViewController = segue.destinationViewController as! DetailViewController
detailViewController.item = self.items[indexPath.row] // like this
}
}
如果tableView属性在另一个类中,并且只有一个section,则可以使用该tag
属性存储单元格的行,例如:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.tag = indexPath.row;
return cell;
}
然后,您可以访问它,因为sender
标记中的行是与该单元格相同的单元格:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
MyDestinationViewController *destinationViewController = segue.destinationViewController;
destinationViewController.myProperty = [tableViewElementsArray objectAtIndex:[sender tag]]; // sender will be your cell
}
self.tableView.indexPathForSelectedRow
返回选定的单元格,但不返回发送方单元格,例如,未选择发送方单元格(辅助操作),或者在多个选择情况下。最好的方法是获取segue发送者的indexPath:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
__auto_type itemViewController = (id<ItemViewController>)segue.destinationViewController;
itemViewController.senderIndexPath = [self.tableView indexPathForCell:sender];
}
在Swift中:
protocol ItemViewController {
var senderIndexPath : IndexPath? { get set }
var selectedIndexPaths : [IndexPath]? { get set }
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let cell = sender as? UITableViewCell,
var itemViewController = segue.destination as? ItemViewController {
itemViewController.senderIndexPath = tableView.indexPath(for: cell)
itemViewController.selectedIndexPaths = tableView.indexPathsForSelectedRows
}
}
如果使用prepareForSegue:
,则可以检查谁是发件人并执行其他代码
例如迅速
override func prepareForSegue(segue: UiStoryboardSegue, sender: AnyObject?)
{
var senderIsTableviewCell:Bool! = sender?.isKindOfClass(UITableViewCell)
if senderIsTableviewCell
{
//do something
}
}