Try changing
if (self.numberOfSections == 1)
to:
if (indexPath.section == 1)
Hope it helps.
Вопрос
I have two arrays. Each Array gets a section. When the user swipes the row and selects delete it must delete the appropriate row in the right section. How do I change my code to get it to delete in the right section. This is my code so far.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//remove the deleted object from your data source.
//If you're data source is an NSMutableArray, do this
if (self.numberOfSections == 1)
{
[self.playerNames removeObjectAtIndex:indexPath.row];
}
else
{
//code here needs to determine which section needs to be deleted
}
[self.tableView reloadData];
}
}
Решение
Try changing
if (self.numberOfSections == 1)
to:
if (indexPath.section == 1)
Hope it helps.
Другие советы
Here is some code snippet:
-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSInteger row = [indexPath row];
[tableDataSource removeObjectAtIndex:row];
[tableView reloadData];
}
}
Also refer this Apple Documentation
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//remove the deleted object from your data source.
//If you're data source is an NSMutableArray, do this
if (self.numberOfSections == 1)
{
[self.playerNames removeObjectAtIndex:indexPath.row];
}
else
{
if (indexPath.section == 0)
{
[self.team1 removeObjectAtIndex:indexPath.row];
}
else
{
[self.team2 removeObjectAtIndex:indexPath.row];
}
}
[self.tableView reloadData];
}
}