1

我的应用程序存储了玩家的数据,我想用它填充 UITableView,但恐怕我有点迷路了。

这是我的代码:

ShowResults.h
#import <UIKit/UIKit.h>

@interface ShowResults : UITableViewController<UITableViewDelegate,UITableViewDataSource, NSFetchedResultsControllerDelegate>

{
    NSManagedObjectContext *managedObjectContext;
    NSFetchedResultsController *fetchedResulstController;
    NSArray *fetchedObjects;
}

@property (nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic) NSManagedObjectContext *managedObjectContext;
@property (nonatomic,strong)  NSArray *fetchedObjects;
@end



ShowResults.m
#import "ShowResults.h"
#import "F1AppDelegate.h"
@interface ShowResults ()
@end

@implementation ShowResults
@synthesize managedObjectContext;
@synthesize fetchedResultsController;
@synthesize fetchedObjects;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

F1AppDelegate *appDelegate =[[UIApplication sharedApplication]delegate];
NSManagedObjectContext *context =[appDelegate managedObjectContext];
NSError *error;

if (managedObjectContext != nil) {
    if ([managedObjectContext hasChanges] &&[managedObjectContext save:&error]) {
        NSLog(@"Unresolved error %@,%@",error, [error userInfo]);  
        abort();
    }
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"Players" inManagedObjectContext:context];

[fetchRequest setEntity:entity];
fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
// Test reading core data

for (NSManagedObject *player in fetchedObjects) {

    NSLog(@"Name: %@", [player valueForKey:@"name"]);
    NSLog(@"Surname: %@", [player valueForKey:@"surname"]);
    NSLog(@"Address: %@", [player valueForKey:@"address"]);
    NSLog(@"Email: %@", [player valueForKey:@"email"]);
    NSLog(@"Phone: %@", [player valueForKey:@"phone"]);
    NSLog(@"City: %@", [player valueForKey:@"city"]);
    NSLog(@"Country: %@", [player valueForKey:@"country"]);
    NSLog(@"Store: %@", [player valueForKey:@"store"]);
    NSLog(@"Age: %@", [player valueForKey:@"age"]);
    NSLog(@"Gender: %@", [player valueForKey:@"gender"]);
    NSLog(@"Time: %@", [player valueForKey:@"time"]);

   }        
// End test
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
     return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSInteger rows = [fetchedObjects count];
    return rows;
 }

- (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];
   }

 NSString *pieceOfData  =[NSString stringWithFormat:@"%@", [fetchedObjects objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:14];   
cell.textLabel.text = pieceOfData;
NSLog(@"cellContent: %@",pieceOfData);
return cell;
}
@end

我在控制台输出上得到的是

Name: Otto
Surname: Von Bismarck
Address: Schuhe, 3
Email: otto@munchen.de
Phone: +34988556633
City: MÜNCHEN
Country: GERMANY
Store: MÜNCHEN
Age: 44
Gender: Male
Time: 03:01:00

cellContent: <Players: 0x6b8f3c0> (entity: Players; id: 0x6b8b450 <x-coredata://C61C85CA-EB53-4B88-87AF-CC45EABFF8ED/Players/p1> ; data: {
address = "Schuhe, 3";
age = 44;
city = "M\U00dcNCHEN";
country = GERMANY;
email = "otto@munchen.de";
gender = Male;
name = Otto;
phone = "+34988556633";
store = "M\U00dcNCHEN";
surname = "Von Bismarck";
time = "03:01:00";
})

你能帮我将这些值写入 tableView 单元格吗?

谢谢 !

4

1 回答 1

5

看起来你只需要做这样的事情:

// in your cellForRowAtIndexPath:
Players *player = [fetchedObjects objectAtIndex:indexPath.row];
// switch email for whatever other property you want to display
cell.textLabel.text = player.email;

看起来您可能需要导入Players.h您的课程。

于 2012-06-19T23:12:33.453 回答