I have two table view filled in their viewDidLoad with data coming from a sqllite db containing names of shops. In the first view i choose one element (i.e. one shop), then in the second i have to choose another shop different from the first.
I can disable one of the row of the second table view after it has been filled?
I tried not to load the name (of the first shop choosen) in the second view but i fill the table view in its viewDidLoad and unfortunately the data regarding the name of the first shop choosen doesn’t seem to be available until the viewDidAppear and that moment is too late to fill the table view.
I tried, also, to add an alert to the event of choosing one row in the second view in this way
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *choosenSecondShop= [secondShop objectAtIndex:indexPath.row];
NSString* nameFirstShop = [function loadFirstShopChoosen];
if ([choosenSecondShop isEqualToString:nameFirstShop]) {
NSString *message=@"Second shop cannot match the first";
alert = [[UIAlertView alloc] initWithTitle:@"Errore"
message: message
delegate: self cancelButtonTitle: @"Ok"
otherButtonTitles:nil,
nil];
[alert show];
}else {
// go on with the app's stuff!
}
}
but in this case the alert is displayed but also the segue followed. I can prevent the segue to go on?
There are a few different ways to attack this problem:
1. Conditional segue
This should work, though I’m not sure it’s a great idea from a user experience perspective.
Presumably you made a segue in IB starting from the table cell? When you do that, you get the convenience of not needing any code to perform the segue (it’s automatically done when you tap the table cell), but you don’t get any run-time control over it either.
If you need programmatic control over whether/when/which segue to perform, you should create a segue starting from the view controller itself (not from a control within it), and give the segue a unique identifier in IB. Then, in your
tableView:didSelectRowAtIndexPath:implementation, you can call[self performSegueWithIdentifier:@"myIdentifier"]once you know you want to perform the segue.2. Disable cell selection
To prevent a specific cell from being selected, your table view controller can implement
tableView:willSelectRowAtIndexPath:to returnnilfor any index path you don’t want the user to select.If you do that, you might want to make it clear to the user which rows can be selected — you can alter the cell’s appearance in
tableView:cellForRowAtIndexPath:.3. Don’t show the cell
You say you’re populating the table view in
viewDidLoad, but it doesn’t really work that way: Table views populate themselves by calling your data source & delegate methods when they need to. If you want to prevent an item from your data set from being shown as a cell in the table, you just need to alter the behavior of your data source & delegate methods:tableView:numberOfRowsInSection:should return a number one less than it would otherwisetableView:cellForRowAtIndexPath:should reflect the removal of the item; something like:Now, if you don’t know which row you want to hide as of the first time these methods are called, all you need to do once you get that information is tell the table view that it needs to call them again:
[self.tableView reloadData]should do the trick.