数组在DidSelectRowAtIndexPath中变为空

时间:2013-04-09 06:46:33

标签: iphone ios objective-c uitableview didselectrowatindexpath

我的coreDatabase中有一个实体“Day”。在一个方法中,我从该实体获取数据并将其放入mutuableArray dayObjects。你可以在这里看到代码。

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

在我的cellForRow中我这样做

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

在我的tableview中,数据显示正确。当我在DidSelectRowAtIndexPath中做同样的事情时,问题就是dat。我log day.p_from时总是得到一个空虚。

有人能帮助我吗?

修改 我的numberOfSections

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

    return sections;
}

我的numberOfRowsInSection

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

我的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;

2 个答案:

答案 0 :(得分:0)

//try
arrDateObjects = [NSMutableArray arrayWithArray:matches];
//instead of
arrDateObjects = [matches mutableCopy];

答案 1 :(得分:0)

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

而不是

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