TableView的展現
.h
#import <UIKit/UIKit.h>
@interface CBTBViewController : UITableViewController
@property (nonatomic,strong) NSManagedObjectContext* managedObjectContext;
@property (strong) NSArray * CBArray;
@property (strong) NSArray * CBImages;
@end
.m
#import <CoreData/CoreData.h>
#import "CBTBViewController.h"
#import "AppDelegate.h"
#import "CrowButterflies.h" //載入資料庫欄位
#import "CBViewController.h"
@interface CBTBViewController ()
@end
NSArray * CBArray;
NSArray * CBImages;
@implementation CBTBViewController
@synthesize managedObjectContext;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// CBArray = [[NSArray alloc] initWithObjects:@"斯氏紫斑蝶",@"端紫斑蝶",@"圓翅紫斑蝶",@"小紫斑蝶",@"大白斑蝶",@"黑脈樺斑蝶",@"樺斑蝶",@"淡紋青斑蝶",@"小紋青斑蝶",@"姬小青斑蝶",@"大青斑蝶",@"小青斑蝶", @"琉球青斑蝶",nil];
CBImages = [[NSArray alloc] initWithObjects:@"crow01-icon.png",@"crow02-icon.png",@"crow03-icon.png",@"crow04-icon.png",@"crow05-icon.png",@"crow06-icon.png",@"crow07-icon.png",@"crow08-icon.png",@"crow09-icon.png",@"crow10-icon.png",@"crow11-icon.png",@"crow12-icon.png",@"crow13-icon.png", nil];
// 載入 core data
if (managedObjectContext == nil)
{
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", managedObjectContext);
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"CrowButterflies" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
// NSLog(@"NSPredicate => %@", areaFilter);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"crow_name != %@", @""];
[fetchRequest setPredicate:predicate];
CBArray = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
// NSLog(@"%@", CBArray);
// 結束 core data
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#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.
// return 0;
return [CBArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
CrowButterflies *c = [CBArray objectAtIndex:indexPath.row];
cell.textLabel.text = c.crow_name;
cell.detailTextLabel.text = c.crow_S_name;
// cell.imageView.image = (NSString *) [CBArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed: (NSString *) [CBImages objectAtIndex:indexPath.row]];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"prepareForSegue");
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
CrowButterflies *p = [CBArray objectAtIndex:path.row];
CBViewController *vc = [segue destinationViewController];
vc.crowbutter = p;
}
@end
沒有留言:
張貼留言