2014-01-10 4 views
0

tableView의 행 내용을 대체해야합니다. 이 경우 행은 스위치 루프에서 채워집니다. 핵심 데이터 엔티티의 개체를 사용하여 텍스트 개체를 변경하려고합니다. :핵심 데이터 개체를 사용하여 텍스트 개체 변경

//cellForRowAtIndexPath  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     } 

     // Configure the cell. 

     switch (indexPath.row) 
     { 
      case 0 : cell.textLabel.text = @"First Cell"; break; 
      case 1 : cell.textLabel.text = @"Second Cell"; break; 
      case 2 : cell.textLabel.text = @"Third Cell"; break; 
      case 3 : cell.textLabel.text = @"Fourth Cell"; break; 
      case 4 : cell.textLabel.text = @"Fifth Cell"; break; 
      case 5 : cell.textLabel.text = @"Sixth Cell"; break; 
      case 6 : cell.textLabel.text = @"Seventh Cell"; break; 
      case 7 : cell.textLabel.text = @"Eighth Cell"; break; 
      default : cell.textLabel.text = [NSString stringWithFormat:@"Cell %i",indexPath.row + 1]; 
     } 

     //cell.detailTextLabel.text = ...; 

     return cell; 
    } 

그리고

//NSFetchedResultsController: 

- (NSFetchedResultsController *)fetchedResultsController { 

    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"ToDoItems" inManagedObjectContext:self.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
           initWithKey:@"tdText" ascending:NO]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 

    [fetchRequest setFetchBatchSize:20]; 

    NSFetchedResultsController *theFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
             managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil 
                cacheName:@"Root"]; 
    self.fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 

} 

답변

0

당신이 마스터 - 세부 응용 프로그램 템플릿을 사용하여 엑스 코드에서 새 프로젝트를 생성 한 후 확인하면 를 사용하여 코어 데이터 : 나는 당신에게이 연루 방법을 보여 확인란을 선택하면 필요한 상용구 코드가 모두 제공됩니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    [self configureCell:cell atIndexPath:indexPath]; 
    return cell; 
} 

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    cell.textLabel.text = [[object valueForKey:@"timeStamp"] description]; 
} 

PS : 예. "ToDoItems"대신 엔티티 "ToDoItem"을 지정하는 것이 더 좋을 것 같습니다.이 경우 복수를 사용하지 마십시오

+0

감사합니다. 답변을 확인해 드리겠습니다. – mvasco

관련 문제