无法将indexPath.row保存到NSUserDefaults

时间:2013-12-31 15:59:48

标签: objective-c uitableview nsuserdefaults nsindexpath

我正在尝试将我可以移动的tableview的行(indexPath.row)保存到NSUserdefaults但是我没有成功,它不起作用或应用程序崩溃.. 什么是缺失/错误?

 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {
        NSString *stringToMove = self.objects[sourceIndexPath.row];
        [self.objects removeObjectAtIndex:sourceIndexPath.row];
        [self.objects insertObject:stringToMove atIndex:destinationIndexPath.row];


    }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //save indexPath 
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        int index = indexPath.row;
        [userDefaults setInteger:index forKey:@"saverows"];
        [userDefaults synchronize];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //load indexPath
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSInteger loadrows = [userDefaults integerForKey:@"saverows"];
        NSIndexPath *newIndex = [NSIndexPath indexPathWithIndex:loadrows];
        self.objects[indexPath.row]=newIndex;

        //rows
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text=self.objects[indexPath.row];
        return cell;
    }

3 个答案:

答案 0 :(得分:1)

  1. 您可以使用这些代码来保存/加载NSIndexPath ...

    // NSUserDefaults+Extension.h
    @interface NSUserDefaults (Extension)
    - (void)setIndexPath:(NSIndexPath *)value forKey:(NSString *)defaultName;
    - (NSIndexPath *)indexPathForKey:(NSString *)defaultName;
    @end
    

    另一个档案......

    // NSUserDefaults+Extension.m
    @implementation NSUserDefaults (Extension)
    - (void)setIndexPath:(NSIndexPath *)value forKey:(NSString *)defaultName
    {
        [self setObject:@{@"row": @(value.row),
                          @"section": @(value.section)}
                 forKey:defaultName];
    }
    - (NSIndexPath *)indexPathForKey:(NSString *)defaultName
    {
        NSDictionary *dict = [self objectForKey:defaultName];
        return [NSIndexPath indexPathForRow:[dict[@"row"] integerValue]
                                  inSection:[dict[@"section"] integerValue]];
    }
    @end
    

    用法:

    #import "NSUserDefaults+Extension.h"
    
    void someFun() {
        NSIndexPath *path = [NSIndexPath indexPathForRow:55 inSection:99];
        NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
        [standardUserDefaults setIndexPath:path forKey:@"myKey"];
        [standardUserDefaults synchronize];
        NSLog(@"%@", [standardUserDefaults indexPathForKey:@"myKey"]);
    }
    
  2. 您的第int index = indexPath.row;行应使用NSInteger代替int

答案 1 :(得分:0)

问题是您尝试添加NSIndexPath作为UILabel的文本

你不能这样做

NSIndexPath *newIndex = [NSIndexPath indexPathWithIndex:loadrows];
self.objects[indexPath.row]=newIndex;

然后这样做

cell.textLabel.text=self.objects[indexPath.row];

在第一部分中,您将添加NSIndexPath,然后将其视为NSString并将其添加到UILabel。


更新1

要保存单元格的顺序,可以保存整个数组(因为它是字符串数组)

moveRowAtIndexPath:方法中,在最后添加

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:self.objects forKey:@"saverows"];
[userDefaults synchronize];
  • 删除save indexPath方法
  • didSelectRowAtIndexPath下您正在执行的操作
  • 删除load indexPath方法
  • cellForRowAtIndexPath下您正在执行的操作

viewDidLoad方法中添加以下内容

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
self.objects = [userDefaults objectForKey:@"saverows"];

答案 2 :(得分:0)

这将为您提供可能的性能问题

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

你正在获得出队,然后用新单元格覆盖它。

if(cell)
  cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

应该清楚。