Use didSelectRowAtIndexPath or prepareForSegue method for UITableView?

前端 未结 6 523
不思量自难忘°
不思量自难忘° 2020-11-29 16:38

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

6条回答
  •  隐瞒了意图╮
    2020-11-29 17:01

    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:

    - (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:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
        MyDestinationViewController *destinationViewController = segue.destinationViewController;
        destinationViewController.myProperty = [tableViewElementsArray objectAtIndex:[sender tag]]; // sender will be your cell 
    }
    

提交回复
热议问题