Section not saving moveRowAtIndexPath objective c

42 Views Asked by At

I've seen a lot of examples, but I still havn't managed to get mine working.

I have 1 array I'm populating from NSUserDefaults. Everything works as it should. However I wanted the ability to move a row into the empty top section. Like pinning a note.

I am able to move and save the rows in their current section, but moving will not apply the change.

Relevant code:

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //if (sourceIndexPath.row != destinationIndexPath.row && sourceIndexPath.section != destinationIndexPath.section) {
    //return;
    if ( sourceIndexPath.row != destinationIndexPath.row )
    {
        id noteObject = [self.noteArray objectAtIndex:sourceIndexPath.row];
        self.noteArray = [noteArray mutableCopy];
        [self.noteArray removeObjectAtIndex:sourceIndexPath.row];
        [self.noteArray insertObject:noteObject atIndex:destinationIndexPath.row];
        
        id dateObject = [self.dateArray objectAtIndex:sourceIndexPath.row];
        self.dateArray = [dateArray mutableCopy];
        [self.dateArray removeObjectAtIndex:sourceIndexPath.row];
        [self.dateArray insertObject:dateObject atIndex:destinationIndexPath.row];
        
        [self performSelector:@selector(delayedReloadData:) withObject:self.tableView afterDelay:0.02];
        return;
    }
}

This gets called if I move rows, but not into the section.

- (void)delayedReloadData:(UITableView *)tableView
{
    [[NSUserDefaults standardUserDefaults] setObject:self.noteArray forKey:@"note"];
    [[NSUserDefaults standardUserDefaults] setObject:self.dateArray forKey:@"date"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self.tableView reloadData];
    NSLog(@"Move Row Update");
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger numOfSections;
    if ([self.noteArray count])
    {
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        numOfSections            = 2;
        tableView.backgroundView = nil;
    }
    else
    {
        UILabel *noDataLabel         = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, tableView.bounds.size.height)];
        noDataLabel.text             = @"No data available";
        numOfSections                = 1;
        noDataLabel.textColor        = [UIColor systemGrayColor];
        noDataLabel.textAlignment    = NSTextAlignmentCenter;
        tableView.backgroundView     = noDataLabel;
        tableView.separatorStyle     = UITableViewCellSeparatorStyleNone;
    }
    
    return numOfSections;
}

This may be the issue? Not sure how to account for an empty section

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return;
    }
    if (section == 1) {
        return [self.noteArray count];
    }
    return section;
}

Any help would be greatly appreciated. Thank You.

0

There are 0 best solutions below