微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

部分不保存 moveRowAtIndexPath 目标 c

如何解决部分不保存 moveRowAtIndexPath 目标 c

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

我有 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,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;
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。