I’m starting with iOS development and I want to implement the following behavior in a table view:
Element 1 of the list cant be selected and when other row is selected the previous element in the list becomes the selected one. I know is senseless, it is just as an exercise. By the way my code is:
-(NSIndexPath *) tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row =[indexPath row];
if (row==0){
return nil;
}
return indexPath;
}
It works for avoiding the row 0 from being selected, now I need change the selected element to the previous when the row!=0. How can I do it?
UITableView has a method named
selectRowAtIndexPath:animated:scrollPosition:(documentation linked for you) where you can select a different row.A couple caveats about this though: you have to select a row via an NSIndexPath (not too hard to set up, once you get the hang of it) and your
willSelectRowAtIndexPathmay be called a second time when you programatically callselectRowAtIndexPathso you need to figure out a way to ignore that call while paying attention to the ones the user does with their finger.