ios 对 UITableView 使用 didSelectRowAtIndexPath 或 prepareForSegue 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8130600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use didSelectRowAtIndexPath or prepareForSegue method for UITableView?
提问by Jon
I'm using storyboards and I have a UITableView. I have a segue setup that pushes from my table to the detail VC. But which method should I use to handle this? I'll have to pass a couple objects to the detail view. But do I use didSelectRowAtIndex
or -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
?
我正在使用故事板并且我有一个 UITableView。我有一个 segue 设置,可以从我的表推送到详细信息 VC。但是我应该使用哪种方法来处理这个问题?我必须将几个对象传递给详细信息视图。但是我使用didSelectRowAtIndex
或-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
吗?
回答by rob mayoff
If you use prepareForSegue:sender:
then you won't have as much to change if you later decide to trigger the segue from some control outside the table view.
如果您使用,prepareForSegue:sender:
那么如果您以后决定从表视图之外的某个控件触发 segue,您将不会有太多更改。
The prepareForSegue:sender:
message is sent to the current view controller, so I'd suggest something like this:
该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];
}
In Swift:
在斯威夫特:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let path = self.tableView.indexPathForSelectedRow()!
segue.destinationViewController.detail = self.detailForIndexPath(path)
}
回答by Rohit Mandiwal
I did this and it worked
我这样做了,它奏效了
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Row Selected = %i",indexPath.row);
[self performSegueWithIdentifier:@"testID" sender:self.view];
}
回答by Kaz Yoshikawa
When sender is UITableViewCell, you may ask UITableView to query indexPath of the cell.
当发送方为 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
}
}
回答by Stanislav P.
self.tableView.indexPathForSelectedRow
returns selected cell, but not segue sender cell, e.g. sender cell is not selected (accessory action), or in multiple selection case. The best way is getting indexPath for segue sender:
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];
}
In 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
}
}
回答by Laszlo
if your tableView property is in another class and you only have one section, then you could use the tag
property to store the cell's row like:
如果您的 tableView 属性在另一个类中并且您只有一个 section,那么您可以使用该tag
属性来存储单元格的行,例如:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.tag = indexPath.row;
return cell;
}
And then you can access it as the sender
is the same cell with the row value in its tag:
然后您可以访问它,因为它sender
与标签中的行值是同一个单元格:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
MyDestinationViewController *destinationViewController = segue.destinationViewController;
destinationViewController.myProperty = [tableViewElementsArray objectAtIndex:[sender tag]]; // sender will be your cell
}
回答by Eugene
If you use prepareForSegue:
you can check who is the sender and execute different code
如果您使用,prepareForSegue:
您可以检查谁是发件人并执行不同的代码
For example in swift
例如在快速
override func prepareForSegue(segue: UiStoryboardSegue, sender: AnyObject?)
{
var senderIsTableviewCell:Bool! = sender?.isKindOfClass(UITableViewCell)
if senderIsTableviewCell
{
//do something
}
}