0

I have a tableview that show saved data, but when I try to go from selected cell, it doesn't show the data that's been saved. It try this

I used NSUserDefaults to save data in other view (another .m file)

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

My problem is that I cant go from a selected cell to the view that the data's been saved.

TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
  • Have you allocate your array: [[globArray alloc] init]; ? – Greg Dec 08 '13 at 14:24
  • What do you see if you put `NSLog(@"%@", globArray);` at the end of the first method and beginning of the second? – Phillip Mills Dec 08 '13 at 14:29
  • The NSDictionary Class ref does not contain a message 'new'. Is this your acual code, or did you transcribe it for this question? This code should throw a compiler error at line 1 of -saveCourseDetail. – katzenhut Dec 08 '13 at 14:33
  • { buildingName = ftvtvt; classRoom = tvtbhy; courseDescription = "uyv\U0131h\U0131u"; courseName = tgrv; teacherName = tbhnun; }, – user3079947 Dec 08 '13 at 14:35
  • It shows me saved datas – user3079947 Dec 08 '13 at 14:35
  • and this is for going to CourseDetailsVC -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if (globArray.count>0) { UITableViewCell *cell=(UITableViewCell*)sender; NSIndexPath *path=[self.table indexPathForCell:cell]; CourseDetailViewController * det = [[CourseDetailViewController alloc] init]; det.savedDataDic = [globArray objectAtIndex:path.row]; } } – user3079947 Dec 08 '13 at 14:37
  • courseDetails.savedDataDic = [globArray objectAtIndex:indexPath.row]; the output shows that all courseDetails elements are nill? but why? – user3079947 Dec 08 '13 at 14:43

1 Answers1

0

I personally recommend you to use core data if there is large amount of data to be stored. Using user defaults is not good to store large chunks of data. Anyways, if you want to store an array of dictionary, you have to encode it and to read the array you have to decode it like this

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

Load:

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

Hope it helps :)

iCode
  • 1,456
  • 1
  • 15
  • 26
sanjaymathad
  • 232
  • 1
  • 5