Question

The code I have below enables the IOS swipe to delete function and deletes the row. The problem is the underlying data structure isn't being deleted. I have 3 cells, Google-Bing-Yahoo, if I delete Google the cell deletes but Bing moves up, becomes indexPath.row 0 and inherits the action of where Google used to be. Where am I missing the "hey delete the action to this cell too" call. Or maybe it's the way I have the cells assigned? Should I be using tags?

////My Array
someData = [[NSMutableArray alloc]init];
    [someData addObjectsFromArray:[NSMutableArray arrayWithObjects: @"Google", @"Bing", @"Yahoo", nil]];


////Allow cell editing & remove data
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

// Remove the row from data model
[someData removeObjectAtIndex:indexPath.row];

// Request table view to reload
[tableView reloadData];

}


////Cell Chosen Action
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


        if(indexPath.row == 0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://google.com"]];
}
        if(indexPath.row == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://bing.com"]];
}
        if(indexPath.row == 2) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://yahoo.com"]];
}
Was it helpful?

Solution

Your didSelectRowAtIndexPath is saying that no matter what, row 0 opens google. When you delete google, row 0 is now bing, but you still open google for it. Do something like:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: [NSString stringWithFormat:@"http://%@.com", [someData objectAtIndex:indexPath.row]]]];
}

Basically you would be forming the url with the name from the array, removing the need for any if statements and confusion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top