2012-06-19 26 views
1

我的應用程序存儲玩家的數據,我想用它填充UITableView,但恐怕我有點迷路。核心數據填充UITableView:無法獲得NSString

這裏是我的代碼:

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: [email protected] 
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 = "[email protected]"; 
gender = Male; 
name = Otto; 
phone = "+34988556633"; 
store = "M\U00dcNCHEN"; 
surname = "Von Bismarck"; 
time = "03:01:00"; 
}) 

你能幫我將這些數值寫入的tableView細胞?

謝謝!

+0

閱讀「NSFetchedResultsController'的教程 – Legolas

回答

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到類。

+0

謝謝!很棒。 – Luis

+0

@路易斯你最好接受答案。 :) – Kjuly

+1

@Kjuly我不得不做一些研究接受答案:) – Luis