2013-10-13 24 views
2

我有一個屬性「組」的實體,得到numberOfRowsInSection因爲我要歸入2段我的實體的「組」列表(0或1)如何從NSFetchedResultsController與sectionNameKeyPath

@property (nonatomic, retain) NSNumber * group; 

在我fetchedResultsController,我指定sectionNameKeyPath爲「組」

self.fetchedResultsController = [[NSFetchedResultsController alloc] 
    initWithFetchRequest:request managedObjectContext:self.managedObjectContext 
    sectionNameKeyPath:@"group" cacheName:nil]; 

我如何可以返回的行數在每個部分下面的方法裏面?我得到以下錯誤:

Terminating app due to uncaught exception 'NSRangeException', reason: 
'-[__NSArrayM objectAtIndex:]: index 1 beyond bounds for empty array' 

下面是代碼:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[[self.fetchedResultsController sections] objectAtIndex:section] 
     numberOfObjects]; 
} 

此外,我想這一點,得到了同樣的錯誤:

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] 
    objectAtIndex:section]; 
return [sectionInfo numberOfObjects]; 

注意,我實現了這個方法,以及:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 2; 
} 

回答

5

您是否實施了numberOfSectionsInTableView:?如果你沒有實現它,tableView假定你有1節,如果fetchedResultsController沒有任何節(即沒有對象),將會導致這個異常。

您必須在numberOfSectionsInTableView:返回[sections count]

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [self.fetchedResultsController.sections count]; 
} 

如果你總是想證明你有來檢查fetchedResultsController具有所要求的部分兩個部分。如果fetchedResultsController沒有此部分,請不要要求此操作中的對象數量。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSInteger count = 0; 
    NSInteger realNumberOfSections = [self.fetchedResultsController.sections count]; 
    if (section < realNumberOfSections) { 
     // fetchedResultsController has this section 
     id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section]; 
     count = [sectionInfo numberOfObjects]; 
    } 
    else { 
     // section not present in fetchedResultsController 
     count = 0; // for empty section, or 1 if you want to show a "no objects" cell. 
    } 
    return count; 
} 

如果在其他情況下返回的值不是0,則必須更改tableView:cellForRowAtIndexPath:。與此方法類似,您必須檢查fetchedResultsController是否在請求的indexPath處具有一個對象。

+0

我實現了這個方法,但我回到具體爲2,因爲我要顯示在現場的第一個視圖2個部分,即使沒有記錄但是 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } – iPhoneJavaDev

+0

你不能這樣做。我會爲此添加一個編輯。 –

+0

如果我沒有關於我想組合的實體的任何記錄,我應該如何強制在開始時顯示2個部分? – iPhoneJavaDev

0

斯威夫特:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    let section = fetchedResults.sections?[section] 
    let sectionCnt = section?.numberOfObjects 

    return sectionCnt! 
} 

首先得到該部分。統計其中的對象數量。

注意:爲此,fetchedResults對象必須知道如何將數據劃分爲多個部分!這是在獲取請求時達到的。在這條線,通過它,你用它來劃分部分CoreData屬性名稱:

fetchedResults = NSFetchedResultsController(
    fetchRequest: fetchRequest, 
    managedObjectContext: managedContext, 
    sectionNameKeyPath: "CD_Attribute_Name_GoesHere", 
    cacheName:nil 
) 
相關問題