使用didSelectRowAtIndexPath来推送已保存的数据viewcontroller

时间:2013-12-08 14:22:35

标签: ios objective-c uitableview didselectrowatindexpath

我有一个显示已保存数据的tableview,但是当我尝试从所选单元格开始时,它不会显示已保存的数据。试试这个

我使用NSUserDefaults将数据保存在其他视图中(另一个.m文件)

- (IBAction)saveCourseDetail:(id)sender
{
NSMutableDictionary *courseDictionary=[NSMutableDictionary new];
[courseDictionary setObject:courseName.text forKey:@"courseName"];
[courseDictionary setObject:courseDescription.text forKey:@"courseDescription"];
[courseDictionary setObject:classRoom.text forKey:@"classRoom"];
[courseDictionary setObject:teacherName.text forKey:@"teacherName"];
[courseDictionary setObject:buildingName.text forKey:@"buildingName"];

[globArray addObject:courseDictionary];


NSUserDefaults *savedData=[NSUserDefaults standardUserDefaults];
[savedData setObject:globArray forKey:@"array"];
// [savedData setObject:courseDictionary forKey:@"courseDictionary"];
[savedData synchronize];


[[NSNotificationCenter defaultCenter] postNotificationName:@"CourseAddedNotification" object:nil];
[self.navigationController popViewControllerAnimated:YES];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    CourseDetailViewController *courseDetails=[[CourseDetailViewController alloc]init] ;


 courseDetails.savedDataDic = [globArray objectAtIndex:indexPath.row];
 [self.navigationController pushViewController: courseDetails animated:YES];
}

我的问题是我无法从选定的单元格转到已保存数据的视图。

1 个答案:

答案 0 :(得分:0)

如果要存储大量数据,我个人建议您使用核心数据。使用用户默认值不适合存储大块数据。无论如何,如果你想存储一个字典数组,你必须对它进行编码并读取你必须解码它的数组

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *arr = ... ; // set value
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr];
[defaults setObject:data forKey:@"theKey"];

负载:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"theKey"];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
The element in the array implements

@interface DictionaryItems : NSObject<NSCoding> {
    NSString *value;
}
Then in the implementation of CommentItem, provides two methods:

-(void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeObject:value forKey:@"Value"];
}

-(id)initWithCoder:(NSCoder *)decoder
{
    self.value = [decoder decodeObjectForKey:@"Value"];
    return self;
}

希望有所帮助:)