部分未保存 moveRowAtIndexPath 目标 c

Section not saving moveRowAtIndexPath objective c

提问人:ChrisOSX 提问时间:5/13/2021 更新时间:5/13/2021 访问量:39

问:

我看过很多例子,但我仍然没有设法让我的工作。

我有 1 个数组,我正在从 NSUserDefaults 填充。一切正常。但是,我希望能够将一行移动到空的顶部。就像固定笔记一样。

我能够移动并保存当前部分中的行,但移动不会应用更改。

相关代码:

- (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;
    }
}

如果我移动行,则调用此函数,但未将其移入该部分。

- (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;
}

这可能是问题所在?不确定如何解释空白部分

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

任何帮助将不胜感激。谢谢。

Objective-C UITableView nsmutableArray nsUserDefaults

评论

0赞 Cy-4AH 5/13/2021
你确定你需要检查吗?不同部分中的项目可以具有相同的行值sourceIndexPath.row != destinationIndexPath.row
0赞 Cy-4AH 5/13/2021
numberOfRowsInSection有奇怪的实现。如果函数期望,则不能返回任何内容。并将部分作为行数返回?NSInteger
0赞 ChrisOSX 5/13/2021
如果我不使用 sourceIndexPath.row != destinationIndexPath.row,它将不允许移动该项目。但是如果我使用它,它会移动但不会保存。

答: 暂无答案