2016-06-04 2 views

답변

0

Apple is clear "배열에있는 개체의 순서는 정의되지 않았습니다." 그래서 나는 그것을 바꿀 수 있다고 기대할 것입니다.

allObjects 배열을 인스턴스 변수에 저장하고이를 사용하여 numberOfSectionsInTableView() 및 다른 데이터 소스 메서드를 구현하는 것이 좋습니다.

당신은 또한 같은 인스턴스 변수에 NSFetchedResultsController을 저장하고 안전하게 사용할 수 있습니다

개인적으로
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    var rows = 0 
    if self.fetchedResultsController!.sections!.count > 0 { 
     let sectionInfo = getSectionInfo(section) 
     rows = sectionInfo!.numberOfObjects 
    } 
    return rows 
} 

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    let sectionInfo = getSectionInfo(section) 
    return sectionInfo!.name 
} 

func getSectionInfo(section: Int) -> NSFetchedResultsSectionInfo? { 
    var sections: [AnyObject] = self.fetchedResultsController!.sections! 
    if section < sections.count { 
     let x = sections[section] as! NSFetchedResultsSectionInfo 
     return x 
    } 
    return nil 
} 

override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int { 
    return self.fetchedResultsController!.sectionForSectionIndexTitle(title, atIndex: index) 
} 

override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? { 
    return self.fetchedResultsController!.sectionIndexTitles 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifier")! as UITableViewCell 

    // Use your own NSManagedObject class here...mine is Category 
    let category = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Category 
    return cell 
} 

난 난 그냥 cellForRowAtIndexPath()을 구현할 수 있도록 재사용 기본 클래스 위의 모든 방법을 코딩 한 각각의 새로운 테이블보기에서. 다른 사람에게 유용 할 수도 있으므로 a copy on github이라고 게시했습니다.

+0

도움 주셔서 감사합니다. 하지만 한 가지 의심의 여지가 allobjects() 메서드는 NSSet을 추가 한 것과 같은 순서로 배열을 반환합니다. –

+0

첫 단락을 추가했습니다. 'allObjects()'순서는 정의되어 있지 않으므로 변경 될 수 있습니다. – user212514

관련 문제