-3

在我的应用程序中,我有两个表格视图。第一张桌子有效。以下是相关方法:

- (void)viewDidLoad
{
    NSURL *url = [NSURL URLWithString:@"http://gdata.youtube.com/feeds/api/users/ruflixnet/playlists?v=2&alt=jsonc"];
    NSData *jsonData = [NSData dataWithContentsOfURL:url];
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"%@",result);
    NSDictionary *objsize = [[NSDictionary alloc] initWithObjectsAndKeys:[[result objectForKey:@"data"] objectForKey:@"totalItems"],@"objsize", nil];
    NSString *size = [objsize objectForKey:@"objsize"];
    _objects = [[ NSMutableArray alloc] init];
    for (int i=1; i<size.integerValue+1; i++) {
        NSDictionary * obj1 = [[NSDictionary alloc] initWithObjectsAndKeys:[[[[result objectForKey:@"data"] objectForKey:@"items"] objectAtIndex:i-1] objectForKey:@"title"],@"title",[[[[result objectForKey:@"data"] objectForKey:@"items"] objectAtIndex:i-1] objectForKey:@"id"],@"detail", nil];
        [_objects  addObject: obj1];}

}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _objects.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.detailTextLabel.text = [[_objects objectAtIndex:indexPath.row]objectForKey:@"detail"];

    return cell;
}

- (NSFetchedResultsController *)fetchedResultsController
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
         // Replace this implementation with code to handle the error appropriately.
         // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return __fetchedResultsController;
}    

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

@end

在第二个表格视图中,当我添加 FetchedResultsController 时,表格视图中没有任何单元格。

#import "Videos.h"
#import "DetailViewController.h"

@interface Videos (){
    NSMutableArray *_objects2;
}
@end

@implementation Videos
@synthesize detailViewController = _detailViewController;


- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *url = [NSURL URLWithString:@"http://gdata.youtube.com/feeds/api/playlists/A99C8B779B3B098F?v=2&alt=jsonc&max-results=50"];
    NSData *jsonData = [NSData dataWithContentsOfURL:url];
    NSError *error;
    id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"%@",error);
    NSDictionary *objsize = [[NSDictionary alloc] initWithObjectsAndKeys:[[result objectForKey:@"data"] objectForKey:@"totalItems"],@"objsize", nil];
    NSString *size = [objsize objectForKey:@"objsize"];
    _objects2 = [[ NSMutableArray alloc] init];
   for (int i=1; i<size.integerValue+1; i++) {
        NSDictionary * obj1 = [[NSDictionary alloc] initWithObjectsAndKeys:[[[[[result objectForKey:@"data"] objectForKey:@"items"] objectAtIndex:1] objectForKey:@"video"] objectForKey:@"title"],@"title",[[[[[result objectForKey:@"data"] objectForKey:@"items"] objectAtIndex:1] objectForKey:@"video"] objectForKey:@"id"],@"detail", nil];
       [_objects2  addObject: obj1];} }

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _objects2.count;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] ;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = [[_objects2 objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.detailTextLabel.text = [[_objects2 objectAtIndex:indexPath.row]objectForKey:@"detail"];

    return cell;
}


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.textLabel.text = [[_objects2 objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.detailTextLabel.text = [[_objects2 objectAtIndex:indexPath.row]objectForKey:@"detail"]; }



@end
4

1 回答 1

4

numberOfSections..在第二个代码转储中的数据源方法中返回 0。这将停止您的表格视图显示任何单元格。

于 2012-06-07T10:33:30.033 回答