可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a UITableViewController with a segue where I'm trying to get the currently selected row:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"EditFilterElementSegue"]){
// Get the element that was clicked and pass it to the view controller for display
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
Element *element = [fetchedResultsController objectAtIndexPath:selectedIndexPath];
FilterElementTableViewController *vc = segue.destinationViewController;
vc.filter = filter;
vc.element = element;
}
}
The problem is indexPathForSelectedRow is returning nil. The docs say nil is returned "if the index path is invalid". If I add:
[self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:0];
selectedIndexPath = [self.tableView indexPathForSelectedRow];
selectedIndexPath is valid. So, I'm currently assuming the row is somehow getting unselected. Can anyone tell me how to figure out if this is the case and what might be causing it?
This code worked fine until I converted to using NSFetchedResultsController (which otherwise is working). I also use indexPathForSelectedRow in several other table view controllers where it works fine (one of which also uses NSFetchedResultsController).
I've also tried breaking in didSelectRowAtIndexPath, but the same thing happens there. (expected since it's called after prepareForSegue.)
回答1:
A possible cause is that you've written something in one of the UITableViewDelegate
methods -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
It's a common practice to deselect the row
which has just been selected.
回答2:
Something that may help others - if you call reloadData()
on your table view, it nilifies the indexPathForSelectedRow
.
回答3:
Be sure, you are not calling deselectRowAtIndexPath
method before getting index path of selected Row.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES]; //add this line after setting indexPath
}
回答4:
Ok In my case, I was calling from UIButton in storyboard by show segue.
And removing following line:
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
with below 2 lines worked for me:
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
hope with my case "calling segue from UIButton on CELL" works for others also.
回答5:
Ok, the bottom line is the scene in storyboard was messed up.
To figure it out I removed the segue and created a brand new empty UITableViewController class and only added enough code so I could click on a row and look at indexPathForSelectedRow. It was still nil.
I then created a new UITableViewController in storyboard and replaced the existing one. Now indexPathForSelectedRow worked.
I copied all the class code over, and the prototype cells over, and it still worked!
Out of curiosity I looked at the storyboard source for the two scenes and noticed that the scene that wasn't working was much longer than the new one. Looking at the storyboard outline it was obvious why, and surprising it worked at all. And, now I remember having trouble cutting & pasting a prototype cell. Guess I should look at the outline mode more often. See the screen shot below (the visual scenes look the same):
![](https://www.manongdao.com/static/images/pcload.jpg)
回答6:
I also had this problem, when I replaced a UITableViewController class with a UIViewController class in code, and didn't update the storyboard i.e. I didn't replace the UITableViewController placeholder in storyboard, with a UIViewController storyboard.
回答7:
Swift 4.2
You have to tell the tableView what cell is selected.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") else { return UITableViewCell() }
let data = products[indexPath.section][indexPath.row]
cell.update(data: data)
// Don't use cell.isSelected = true
if data == selectedData {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
return cell
}