【问题标题】:Logic issues with numberOfItemsInSection:numberOfItemsInSection 的逻辑问题:
【发布时间】:2013-11-13 02:14:36
【问题描述】:

我正在尝试在此处制作我自己的本教程版本(UITableViewCell 中的 UICollectionView):http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell

我很难在每个 UITableViewCell 中获得正确的 UICollectionViewCells 计数:

第一个单元格是正确的,它应该在 UICollectionView 内部有两个 UICollectionViewCells(当然,在 UITableViewCell 内部)。第二个是什么让我失望。每次我在 Core Data 中进行新的保存时,它都会将其添加到以前的单元格中。第二个单元格应该有一个 UICollectionViewCell,而不是两个 UICollectionViewCell。

这是我的代码(AFTableViewCell 和 AFIndexedCollectionView 的代码可以在上面的教程链接中找到):

numberOfItemsInSection:对于嵌入在 UITableViewCell 中的 UICollectionView:

- (NSInteger)collectionView:(AFIndexedCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
NSIndexPath *indexPath;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:managedObjectContext];

[fetchRequest setEntity:entity];

NSSortDescriptor *urlDescriptor = [[NSSortDescriptor alloc] initWithKey:@"url" ascending:NO];

NSArray *sortDescriptors = nil;

sortDescriptors = [[NSArray alloc] initWithObjects:urlDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

NSError *error = nil;

NSArray *fetchedObjects = [[managedObjectContext executeFetchRequest:fetchRequest error:&error]objectAtIndex:indexPath.section];

NSString *temp = [fetchedObjects description];

NSArray *tempArg = [temp componentsSeparatedByString:@","];

return [tempArg count];
} 

the numberOfSectionsInTableView: for main UITableView:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
id <NSFetchedResultsSectionInfo> sectionInfo = nil;

NSIndexPath *section;

sectionInfo = [[fetchedResultsController sections] objectAtIndex:section.section];

return [sectionInfo numberOfObjects]; 
}

NSFetchedResultsController:

- (NSFetchedResultsController *)fetchedResultsController 
{ 
TBAppDelegate *delegate = (TBAppDelegate *)[[UIApplication sharedApplication] delegate];

self.managedObjectContext = delegate.managedObjectContext;

NSFetchRequest *fetchRequest = nil;

fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = nil;

entity = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:managedObjectContext];

[fetchRequest setEntity:entity];

[fetchRequest setFetchBatchSize:INFINITY];

NSSortDescriptor *urlDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];

NSArray *sortDescriptors = nil;

sortDescriptors = [[NSArray alloc] initWithObjects:urlDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *frc = nil;

frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:@"Root"];

[frc setDelegate:self];

[self setFetchedResultsController:frc];

return frc;

}

UITableView cellForRowAtIndexpath:

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

AFTableViewCell *cell = (AFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell)
{
    cell = [[AFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

return cell;   
}

提前致谢。

【问题讨论】:

    标签: ios core-data uitableview uicollectionview


    【解决方案1】:

    使用

    NSArray *fetchedObjects = [[managedObjectContext executeFetchRequest:fetchRequest error:&error]objectAtIndex:section];
    

    而不是

    NSArray *fetchedObjects = [[managedObjectContext executeFetchRequest:fetchRequest error:&error]objectAtIndex:indexPath.section];
    

    在第 10 行的 - (NSInteger)collectionView:(AFIndexedCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 函数中

    似乎 IndexPath 局部变量未在此函数中初始化。您可以从传递的参数中获取section。

    你还没有在你的

    中初始化section局部变量
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    

    功能。

    如果您不初始化并使用这些变量,它们会返回垃圾值或使程序崩溃。此类访问要小心。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 2018-06-08
      相关资源
      最近更新 更多