سؤال

I have in my coreDatabase an entity "Day". In a method I fetch the data from that entity and put it in a mutuableArray dayObjects. You can see the code over here.

  NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext;
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Day"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"p_date >= %@ and p_date <= %@",dateString,dateString2];
    [fetchRequest setPredicate:predicate];
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"p_id" ascending:YES];
    fetchRequest.sortDescriptors = @[descriptor];
    NSArray *matches = [context executeFetchRequest:fetchRequest error:nil];
    NSLog(@"matches = %@",[matches valueForKey:@"p_date"]);
    arrDateObjects = [matches mutableCopy];

In my cellForRow I do this

  Day *objDay = [arrDateObjects objectAtIndex:indexPath.row-1];
  Cell.titlelabel.text = day.p_from;

In my tableview the data is showed correctly. The problem is dat when I do the same thing in my DidSelectRowAtIndexPath. I always get a Null when I log day.p_from.

Can anybody help me ?

EDIT My numberOfSections

 (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    int sections = 0;
     sections = 1;

    return sections;
}

My numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int rows = 0;
    rows = 8; //showing 7 days + 1 row for a little title
    return rows;
}

My CellForRowAtIndexPath

static NSString *simpleTableIdentifier = @"OpeningCell";

OpeningCell *cell = (OpeningCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OpeningCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

if(indexPath.row == 0){
    cell.lblDay.text = NSLocalizedString(@"lblDag", nil);
    cell.lblFrom.text = NSLocalizedString(@"lblVan", nil);
    cell.lblTill.text = NSLocalizedString(@"lblTot", nil);
}else{
    Day *objDay = [arrDateObjects objectAtIndex:indexPath.row-1];
    NSLog(@"Day object in cell from = %@",objDay.p_from);
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"YYYY-MM-dd"];
    NSDate *date = [dateFormat dateFromString:objDay.p_date];
    NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
    [dateFormat2 setDateFormat:@"dd/MM"];
    NSString *dateString = [dateFormat2 stringFromDate:date];
    NSLog(@"date: %@", dateString);

    cell.lblShort.text = dateString;
    cell.lblDay.text = [arrDays objectAtIndex:indexPath.row-1];
    NSString *txtFrom = [objDay.p_from substringWithRange:NSMakeRange(0, 5)];
    cell.lblFrom.text = txtFrom;
    NSString *txtTill = [objDay.p_till substringWithRange:NSMakeRange(0, 5)];
    cell.lblTill.text = txtTill;
}
return cell;
هل كانت مفيدة؟

المحلول

 NSLog(@"matches = %@",[matches valueForKey:@"p_date"]);
    arrDateObjects=[[NSMutableArray alloc]init];
    [arrDateObjects addobject:[matches objectAtindex:indexpath.row-1]];

Instead of

  NSLog(@"matches = %@",[matches valueForKey:@"p_date"]);
    arrDateObjects = [matches mutableCopy];

نصائح أخرى

//try
arrDateObjects = [NSMutableArray arrayWithArray:matches];
//instead of
arrDateObjects = [matches mutableCopy];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top