実際使った感想はシンプルですが動きがスタイリッシュで使っていて楽しくさせてくれるアプリケーションですね。
ToDo管理や買い物メモなどとして使えそうです。
価格は200円(2012/5/31現在)です。
- Clear for iPhoneホームページ
iPhoneSDK:5.1
Xcodeバージョン:4.3.2
iPhoneシュミレーター:5.0.1
新規プロジェクトを作成します。
#import@interface Artist : NSObject { NSString *id; NSString *name; NSString *genre; } @property(nonatomic,retain)NSString *id; @property(nonatomic,retain)NSString *name; @property(nonatomic,retain)NSString *genre; @end
#import "Artist.h" @implementation Artist @synthesize id; @synthesize name; @synthesize genre; @end続いてTable View Controllerを記述していきます。
<MasterTableViewController.m>#import "FMDatabase.h" @interface MasterTableViewController : UITableViewController { NSMutableArray *mArtists; } @end
#import "MasterTableViewController.h"
#import "Artist.h"
@interface MasterTableViewController ()
@end
@implementation MasterTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Master";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Sample.db"];
BOOL success = [fileManager fileExistsAtPath:writableDBPath];
if(!success){
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Sample.db"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
}
if(!success){
NSAssert1(0, @"failed to create writable db file with message '%@'.", [error localizedDescription]);
}
FMDatabase* db = [FMDatabase databaseWithPath:writableDBPath];
if(![db open])
{
NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
}
[db setShouldCacheStatements:YES];
NSString* sql = @"SELECT * FROM Artist;";
FMResultSet* rs = [db executeQuery:sql];
mArtists = [[NSMutableArray alloc] init];
while( [rs next] )
{
Artist * artist = [[Artist alloc] init];
artist.id = [rs intForColumn:@"ArtistID"];
artist.name = [rs stringForColumn:@"ArtistName"];
artist.genre = [rs stringForColumn:@"Genre"];
artist.origin = [rs stringForColumn:@"Origin"];
artist.yearsActive = [rs stringForColumn:@"YearsActive"];
[mArtists addObject:artist];
}
[rs close];
[db close];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#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 [mArtists count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
Artist *artist = [mArtists objectAtIndex:indexPath.row];
cell.textLabel.text = artist.name;
cell.detailTextLabel.text = artist.genre;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
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:[NSArray arrayWithObject: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];
*/
}
@end
ここまでで実行すると以下のような画面が表示されると思います。
#import "Artist.h"
@interface DetailViewController : UIViewController
{
Artist *artist;
UILabel *name;
UILabel *genre;
UILabel *origin;
UILabel *yearsActive;
}
@property (nonatomic,retain) Artist *artist;
@property (nonatomic,retain) IBOutlet UILabel *name;
@property (nonatomic,retain) IBOutlet UILabel *genre;
@property (nonatomic,retain) IBOutlet UILabel *origin;
@property (nonatomic,retain) IBOutlet UILabel *yearsActive;
@end
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
@synthesize artist;
@synthesize name;
@synthesize genre;
@synthesize origin;
@synthesize yearsActive;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
name.text = artist.name;
genre.text = artist.genre;
origin.text = artist.origin;
yearsActive.text = artist.yearsActive;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
#import "DetailViewController.h"
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailView"];
detailViewController.title = @"Detail";
detailViewController.artist = [mArtists objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
}
できあがり