since: 2011/09/09
update: 2011/12/02
A. 修改 ArticleListViewController.m 如下:
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
//return 0;
// it always return 1;
return [[self.fetchedResultsController sections] count];
}
---------------------------------------------------------------------------------------------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
//return 0;
id<NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
---------------------------------------------------------------------------------------------------------------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = @"Cell";
static NSString *CellIdentifier = @"ArticleListCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
// * need to implement the configureCell:atIndexPath: method
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
B. 在 ArticleListViewController.m 裡, 實作 configureCell:atIndexPath: method
1. 在 @implementation 字串的前一行宣告一個區塊式的 private method:
@interface ArticleListViewController()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
@implementation ArticleListViewController
2. 接著在實作區, 實作此 method:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *articleList = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [articleList valueForKey:@"title"];
cell.textLabel.textColor = [UIColor brownColor];
}
C. 處理: 當使用者移動或刪除一列資料 (in this case, we can only delete it)
1. 在 ArticleListViewController.h 新增以下的 method:
- (void)saveContext;
2. 在 ArticleListViewController.m 新增以下的 method:
- (void)saveContext {
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
---------------------------------------------------------------------------------------------------------------
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object for the given index path
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context
[self saveContext];
}
}
D. 在 NoteBookViewController.m 中提供 managed object contexts 給 articleListViewController :
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
ArticleListViewController *articleListViewController = [[ArticleListViewController alloc] initWithStyle:UITableViewStylePlain];
// set controller's managed object context member right after initializing
articleListViewController.managedObjectContext = self.managedObjectContext;
NSManagedObject *noteBook = [[self fetchedResultsController] objectAtIndexPath:indexPath];
articleListViewController.title = [NSString stringWithFormat:@"%@",[noteBook valueForKey:@"name"]];
//@add for relationship used
articleListViewController.noteBook = noteBook;
// not need to Wrap it in an instance of UINavigationController, just do:
[self.navigationController pushViewController:articleListViewController animated:YES];
[articleListViewController release];
}
2011年9月9日 星期五
Lala's Program Note 實作記錄: 12. 為文章列表實作 NSFetchedResultsControllerDelegate 的 methods.
since: 2011/09/09
update: 2011/09/09
在 ArticleListViewController.m 實作以下的 methods:
#pragma mark -
#pragma mark Fetched results controller delegate
// * tells this controller's table view to start a series of updates
// * it can initiate a sequence of changes through its beginUpdates: method.
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
---------------------------------------------------------------------------------------------------------------
// * is called when the changes are done.
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
---------------------------------------------------------------------------------------------------------------
// only for supports sections (目前沒用到)
// * is called when a section changes(added or deleted)
/*
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
*/
---------------------------------------------------------------------------------------------------------------
// * is called when a managed object changes(added, deleted, edited, or moved)
// * 需要再配合之後實作 configureCell:atIndexPath: method
// * configureCell:atIndexPath: method will be responsible for configuring a single table cell,
// and you will reuse this method in the tableView:cellForRowAtIndexPath: method.
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch (type) {
// An object has been added (inserted)
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
// An object has been deleted
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
// An object has been updated (edited)
case NSFetchedResultsChangeUpdate:
// need to implement the configureCell:atIndexPath: method
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
// An object has been moved (not used here)
/*
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
*/
}
}
update: 2011/09/09
在 ArticleListViewController.m 實作以下的 methods:
#pragma mark -
#pragma mark Fetched results controller delegate
// * tells this controller's table view to start a series of updates
// * it can initiate a sequence of changes through its beginUpdates: method.
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
---------------------------------------------------------------------------------------------------------------
// * is called when the changes are done.
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
---------------------------------------------------------------------------------------------------------------
// only for supports sections (目前沒用到)
// * is called when a section changes(added or deleted)
/*
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
*/
---------------------------------------------------------------------------------------------------------------
// * is called when a managed object changes(added, deleted, edited, or moved)
// * 需要再配合之後實作 configureCell:atIndexPath: method
// * configureCell:atIndexPath: method will be responsible for configuring a single table cell,
// and you will reuse this method in the tableView:cellForRowAtIndexPath: method.
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch (type) {
// An object has been added (inserted)
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
// An object has been deleted
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
// An object has been updated (edited)
case NSFetchedResultsChangeUpdate:
// need to implement the configureCell:atIndexPath: method
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
// An object has been moved (not used here)
/*
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
*/
}
}
標籤:
開發筆記,
iphone,
Lala's Program Note
Lala's Program Note 實作記錄: 11. 為文章列表加上 NSFetchedResultsController
since: 2011/09/09
update: 2011/09/09
A. 為文章列表(ArticleList) 加上 NSFetchedResultsController
1. 修改 ArticleListViewController.h 如下:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
//@interface ArticleListViewController : UITableViewController
@interface ArticleListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
//@for relationship used
NSManagedObject *noteBook;
}
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSManagedObject *noteBook;
@end
---------------------------------------------------------------------------------------------------------------
2. 修改 ArticleListViewController.m 如下:
@synthesize fetchedResultsController, managedObjectContext, noteBook;
- (void)dealloc {
[fetchedResultsController release];
[managedObjectContext release];
[noteBook release];
[super dealloc];
}
---------------------------------------------------------------------------------------------------------------
3. 在 ArticleListViewController.m 建立 Fetched Results Controller
#pragma mark -
#pragma mark Fetched results controller
// Creating the Fetched Results Controller:
// Do it in the accessor for the fetchedResultsController member
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
// Create the fetch request for the entity
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"NoteArticle" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size
[fetchRequest setFetchBatchSize:10];
// sort by noteBook name, ascending
// * with Fetched Results Controller, you must have at least one sort descriptor.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// for relationship
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"notebook == %@", self.noteBook];
[fetchRequest setPredicate:predicate];
// Create the fetched results controller using the fetch request we just created,
// and with the managed object context member,
// and set this controller to be the delegate
// * we pass nil for the sectionNameKeyPath parameter,
// because we have no sections.
// **** 若將 cacheName 設為 @"NoteArticle", 會出現以下的錯誤訊息; 因此將 cacheName 設為 nil ****
/*
currFATAL ERROR The persistent cache of section information does not match the ent configuration You have illegally mutated the NSFetchedResultsController's fetch request its predicate or its sort descriptor without either disabling caching
*/
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
// set ArticleListViewController be aFetchedResultsController's delegate
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
// Clean up
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
// Fetch the results into the fetched results controller
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
//TODO
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return fetchedResultsController;
}
---------------------------------------------------------------------------------------------------------------
4. 修改 NoteBookViewController.m 檔案
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
//ArticleListViewController *articleListViewController = [[ArticleListViewController alloc] init];
ArticleListViewController *articleListViewController = [[ArticleListViewController alloc] initWithStyle:UITableViewStylePlain];
// set controller's managed object context member right after initializing
articleListViewController.managedObjectContext = self.managedObjectContext;
NSManagedObject *noteBook = [[self fetchedResultsController] objectAtIndexPath:indexPath];
articleListViewController.title = [NSString stringWithFormat:@"%@",[noteBook valueForKey:@"name"]];
//@add for relationship used
articleListViewController.noteBook = noteBook;
//@update
// not need to Wrap it in an instance of UINavigationController, just do:
[self.navigationController pushViewController:articleListViewController animated:YES];
[articleListViewController release];
}
update: 2011/09/09
A. 為文章列表(ArticleList) 加上 NSFetchedResultsController
1. 修改 ArticleListViewController.h 如下:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
//@interface ArticleListViewController : UITableViewController
@interface ArticleListViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
//@for relationship used
NSManagedObject *noteBook;
}
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSManagedObject *noteBook;
@end
---------------------------------------------------------------------------------------------------------------
2. 修改 ArticleListViewController.m 如下:
@synthesize fetchedResultsController, managedObjectContext, noteBook;
- (void)dealloc {
[fetchedResultsController release];
[managedObjectContext release];
[noteBook release];
[super dealloc];
}
---------------------------------------------------------------------------------------------------------------
3. 在 ArticleListViewController.m 建立 Fetched Results Controller
#pragma mark -
#pragma mark Fetched results controller
// Creating the Fetched Results Controller:
// Do it in the accessor for the fetchedResultsController member
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
// Create the fetch request for the entity
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"NoteArticle" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size
[fetchRequest setFetchBatchSize:10];
// sort by noteBook name, ascending
// * with Fetched Results Controller, you must have at least one sort descriptor.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// for relationship
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"notebook == %@", self.noteBook];
[fetchRequest setPredicate:predicate];
// Create the fetched results controller using the fetch request we just created,
// and with the managed object context member,
// and set this controller to be the delegate
// * we pass nil for the sectionNameKeyPath parameter,
// because we have no sections.
// **** 若將 cacheName 設為 @"NoteArticle", 會出現以下的錯誤訊息; 因此將 cacheName 設為 nil ****
/*
currFATAL ERROR The persistent cache of section information does not match the ent configuration You have illegally mutated the NSFetchedResultsController's fetch request its predicate or its sort descriptor without either disabling caching
*/
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
// set ArticleListViewController be aFetchedResultsController's delegate
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
// Clean up
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];
// Fetch the results into the fetched results controller
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
//TODO
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return fetchedResultsController;
}
---------------------------------------------------------------------------------------------------------------
4. 修改 NoteBookViewController.m 檔案
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
//ArticleListViewController *articleListViewController = [[ArticleListViewController alloc] init];
ArticleListViewController *articleListViewController = [[ArticleListViewController alloc] initWithStyle:UITableViewStylePlain];
// set controller's managed object context member right after initializing
articleListViewController.managedObjectContext = self.managedObjectContext;
NSManagedObject *noteBook = [[self fetchedResultsController] objectAtIndexPath:indexPath];
articleListViewController.title = [NSString stringWithFormat:@"%@",[noteBook valueForKey:@"name"]];
//@add for relationship used
articleListViewController.noteBook = noteBook;
//@update
// not need to Wrap it in an instance of UINavigationController, just do:
[self.navigationController pushViewController:articleListViewController animated:YES];
[articleListViewController release];
}
標籤:
開發筆記,
iphone,
Lala's Program Note
2011年9月3日 星期六
Lala's Program Note 實作記錄: 10. 自定 TableViewCell 的 accessoryView 來切換畫面
since: 2011/09/03
update: 2011/12/01
A. 說明:
由於已將在 TableView 上輕拍(Tap) 儲存格(Cell), 作成編輯的功能,
因此改利用觸碰(touch) Cell 的 accessoryView 來觸發畫面的切換.
----------------------------------------------------------------------------------------------------------
B. 新增要切換的畫面:
1. 專案目錄 > New File >
2. iOS > Cocoa Touch > UIViewController subclass > Next >
3. Subclass 選擇: UITableViewController, 並勾選(預設): With XIB for user interface
> Next
4. Save As: ArticleListViewController.m
----------------------------------------------------------------------------------------------------------
C. 在 NoteBookViewController.h 裡, 宣告以下的 method:
- (void)arrowBtnClicked:(id)sender event:(id)event;
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
----------------------------------------------------------------------------------------------------------
D. 在 NoteBookViewController.m 裡, 定義以下的 method:
#import "ArticleListViewController.h"
- (void)arrowBtnClicked:(id)sender event:(id)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if (indexPath != nil)
{
[self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
ArticleListViewController *articleListViewController = [[ArticleListViewController alloc] init];
NSManagedObject *noteBook = [[self fetchedResultsController] objectAtIndexPath:indexPath];
articleListViewController.title = [NSString stringWithFormat:@"%@",[noteBook valueForKey:@"name"]];
[self.navigationController pushViewController:articleListViewController animated:YES];
[articleListViewController release];
}
----------------------------------------------------------------------------------------------------------
E. 在 NoteBookViewController.m 裡, 修改以下的 method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NoteBookCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//@add
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
UIImage *image = [UIImage imageNamed:@"iArrowRight-50.png"];
UIButton *button;
button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake( 0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
[button addTarget:self action:@selector(arrowBtnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
}
// Configure the cell...
// * need to implement the configureCell:atIndexPath: method
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
update: 2011/12/01
由於已將在 TableView 上輕拍(Tap) 儲存格(Cell), 作成編輯的功能,
因此改利用觸碰(touch) Cell 的 accessoryView 來觸發畫面的切換.
----------------------------------------------------------------------------------------------------------
B. 新增要切換的畫面:
1. 專案目錄 > New File >
2. iOS > Cocoa Touch > UIViewController subclass > Next >
3. Subclass 選擇: UITableViewController, 並勾選(預設): With XIB for user interface
> Next
4. Save As: ArticleListViewController.m
----------------------------------------------------------------------------------------------------------
C. 在 NoteBookViewController.h 裡, 宣告以下的 method:
- (void)arrowBtnClicked:(id)sender event:(id)event;
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
----------------------------------------------------------------------------------------------------------
D. 在 NoteBookViewController.m 裡, 定義以下的 method:
#import "ArticleListViewController.h"
- (void)arrowBtnClicked:(id)sender event:(id)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
if (indexPath != nil)
{
[self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
ArticleListViewController *articleListViewController = [[ArticleListViewController alloc] init];
NSManagedObject *noteBook = [[self fetchedResultsController] objectAtIndexPath:indexPath];
articleListViewController.title = [NSString stringWithFormat:@"%@",[noteBook valueForKey:@"name"]];
[self.navigationController pushViewController:articleListViewController animated:YES];
[articleListViewController release];
}
----------------------------------------------------------------------------------------------------------
E. 在 NoteBookViewController.m 裡, 修改以下的 method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NoteBookCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//@add
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
UIImage *image = [UIImage imageNamed:@"iArrowRight-50.png"];
UIButton *button;
button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake( 0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
[button addTarget:self action:@selector(arrowBtnClicked:event:) forControlEvents:UIControlEventTouchUpInside];
}
// Configure the cell...
// * need to implement the configureCell:atIndexPath: method
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
標籤:
開發筆記,
iphone,
Lala's Program Note
Lala's Program Note 實作記錄: 9. 在 TableViewCell 中加入 subtitle
since: 2011/09/03
update: 2011/12/01

A. 在 NoteBookViewController.m 中, 調整以下的 method 內容:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"NoteBookCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
....
}
// Configure the cell...
// * need to implement the configureCell:atIndexPath: method
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
B. 在 NoteBookViewController.m 中, 調整以下的 method 內容:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *noteBook = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [noteBook valueForKey:@"name"];
cell.textLabel.textColor = [UIColor brownColor];
cell.detailTextLabel.text = [noteBook valueForKey:@"subName"];
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
}
標籤:
開發筆記,
iphone,
Lala's Program Note
Lala's Program Note 實作記錄: 8. 調整: 刪除 NoteBook 的按鈕機制
since: 2011/09/03
update: 2011/09/03
A. 說明:
1. 原本的刪除功能是在 Edit 與 Done 之間切換, 易讓使用者混淆.
B. 作法:
1. 在 NoteBookViewController.h 宣告一個用來切換垃圾桶與 Done 狀態的 method.
- (void)toggleDelete;
2. 在 NoteBookViewController.m 定義此 method 的內容.
- (void)toggleDelete {
/* 切換編輯狀態 */
[self setEditing:!self.editing animated:YES];
/* 正在編輯(刪除)中 */
if (self.editing) {
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(toggleDelete)];
self.navigationItem.leftBarButtonItem = doneButton;
[doneButton release];
}
/* 尚未編輯(刪除) */
else {
UIBarButtonItem *trashButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self action:@selector(toggleDelete)];
self.navigationItem.leftBarButtonItem = trashButton;
[trashButton release];
}
}
3. 修改 NoteBookViewController.m 內 viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
// 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;
// 實際上是進入刪除的功能
//@deleteButton
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(toggleDelete)];
self.navigationItem.leftBarButtonItem = deleteButton;
[deleteButton release];
//@addButton
....
}
update: 2011/09/03
A. 說明:
1. 原本的刪除功能是在 Edit 與 Done 之間切換, 易讓使用者混淆.
1. 在 NoteBookViewController.h 宣告一個用來切換垃圾桶與 Done 狀態的 method.
- (void)toggleDelete;
2. 在 NoteBookViewController.m 定義此 method 的內容.
- (void)toggleDelete {
/* 切換編輯狀態 */
[self setEditing:!self.editing animated:YES];
/* 正在編輯(刪除)中 */
if (self.editing) {
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(toggleDelete)];
self.navigationItem.leftBarButtonItem = doneButton;
[doneButton release];
}
/* 尚未編輯(刪除) */
else {
UIBarButtonItem *trashButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self action:@selector(toggleDelete)];
self.navigationItem.leftBarButtonItem = trashButton;
[trashButton release];
}
}
3. 修改 NoteBookViewController.m 內 viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
// 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;
// 實際上是進入刪除的功能
//@deleteButton
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(toggleDelete)];
self.navigationItem.leftBarButtonItem = deleteButton;
[deleteButton release];
//@addButton
....
}
標籤:
開發筆記,
iphone,
Lala's Program Note
2011年9月2日 星期五
Lala's Program Note 實作記錄: 7. 建立:新增與編輯 NoteBook 的介面
since: 2011/09/02
update: 2011/09/02
A. 新增 "新增與編輯" 的介面:
1. 專案目錄 > New File >
2. iOS > Cocoa Touch > UIViewController subclass > Next >
3. Subclass 選擇: UIViewController, 並勾選(預設): With XIB for user interface > Next
4. Save As: ManageNBViewController.m
----------------------------------------------------------------------------------------------------------------
B. 開啟 ManageNBViewController.h 修改如下:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@class NoteBookViewController; // 要當作 parenet Controller
@interface ManageNBViewController : UIViewController {
IBOutlet UITextField *nameField; // NoteBook 的名稱
IBOutlet UITextField *subNameField; // NoteBook 的副名稱
NoteBookViewController *parenetController; // A poniter to parent controller
NSManagedObject *noteBook; // represents the current note
}
@property (nonatomic, retain) IBOutlet UITextField *nameField;
@property (nonatomic, retain) IBOutlet UITextField *subNameField;
@property (nonatomic, retain) NoteBookViewController *parenetController;
@property (nonatomic, retain) NSManagedObject *noteBook;
- (id)initWithParentController:(NoteBookViewController *)aParentController noteBook:(NSManagedObject *)aNoteBook;
- (IBAction)save:(id)sender;
- (IBAction)cancel:(id)sender;
----------------------------------------------------------------------------------------------------------------
C. 開啟 ManageNBViewController.m 修改如下:
#import "ManageNBViewController.h"
#import "NoteBookViewController.h"
@synthesize nameField, subNameField, parenetController, noteBook;
- (id)initWithParentController:(NoteBookViewController *)aParentController noteBook:(NSManagedObject *)aNoteBook
{
if ((self = [super init])) {
self.parenetController = aParentController;
self.noteBook = aNoteBook;
}
return self;
}
- (IBAction)save:(id)sender {
if (parenetController != nil) {
/* editing */
if (noteBook != nil) {
[noteBook setValue:nameField.text forKey:@"name"];
[noteBook setValue:subNameField.text forKey:@"subName"];
//now date
[noteBook setValue:[NSDate date] forKey:@"dateUpdate"];
// Save the context
[parenetController saveContext];
}
/* creating a new noteBook */
else {
[parenetController insertNoteBookWithName:nameField.text subName:subNameField.text];
}
}
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)cancel:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if (noteBook != nil) {
nameField.text = [noteBook valueForKey:@"name"];
subNameField.text = [noteBook valueForKey:@"subName"];
}
else {
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.nameField = nil;
self.subNameField = nil;
}
- (void)dealloc {
[parenetController release];
[noteBook release];
[super dealloc];
}
----------------------------------------------------------------------------------------------------------------
D. 建立實際的 UI:
開啟 ManageNBViewController.xib 檔案, 修改如下:
1. 拖拉一個 Navigation Bar 到 view 上, 對齊 view 最上方的中間,
將 title 改為: NoteBook.
2. 拖拉二個 Bar Button Item 到 Navigation Bar 上, 一個放在左邊, 一個放在右邊.
(1). 分別更改它們的 label, 左邊為: Canel, 右邊為: Save
(2). 並分別與 File's Owner 上的 cancel: 與 save: 作連結.
3. 拖拉一個 Label 到 view 的左上方, Navigation Bar 的下面, 將其 text 改為: Name:
4. 拖拉一個 TextField 到 Name: Label 的右方, 並與 File's Owner
上的 nameField 作連結.
5. 拖拉一個 Label 到 Name: Label 的下方, 將其 text 改為: SubName:
6. 拖拉一個 TextField 到 SubName: Label 的右方, 並與 File's Owner
上的 subNameField 作連結.
----------------------------------------------------------------------------------------------------------------
E. 開啟 NoteBookViewController.h 檔案, 新增二個 method 如下:
- (void)showManageNBView;
- (void)insertNoteBookWithName:(NSString *)name subName:(NSString *)subName;
----------------------------------------------------------------------------------------------------------------
F. 開啟 NoteBookViewController.m 檔案, 新增二個 method 如下:
#import "NoteBookViewController.h"
#import "ManageNBViewController.h"
- (void)showManageNBView {
ManageNBViewController *manageNBViewController = [[ManageNBViewController alloc]initWithParentController:self noteBook:nil]; // so a new noteBook will be created
[self presentModalViewController:manageNBViewController animated:YES];
[manageNBViewController release];
}
- (void)insertNoteBookWithName:(NSString *)name subName:(NSString *)subName {
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
NSManagedObject *newNoteBook = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newNoteBook setValue:name forKey:@"name"];
[newNoteBook setValue:subName forKey:@"subName"];
//Date
[newNoteBook setValue:[NSDate date] forKey:@"dateCreated"];
[newNoteBook setValue:[NSDate date] forKey:@"dateUpdate"];
// Save the context
[self saveContext];
}
----------------------------------------------------------------------------------------------------------------
G. 幫 NoteBook 介面增加 "新增" 與 "編輯" 的按鈕.
開啟 NoteBookViewController.m 檔案, 修改如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// 實際上是進入刪除的功能
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showManageNBView)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}
----------------------------------------------------------------------------------------------------------------
H. 增加: 編輯 NoteBook 功能:
開啟 NoteBookViewController.m 檔案, 修改如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *noteBook = [[self fetchedResultsController] objectAtIndexPath:indexPath];
ManageNBViewController *manageNBViewController = [[ManageNBViewController alloc] initWithParentController:self noteBook:noteBook];
[self presentModalViewController:manageNBViewController animated:YES];
[manageNBViewController release];
}
----------------------------------------------------------------------------------------------------------------
- (IBAction)didEndEdit:(id)sender;
2. 開啟 ManageNBViewController.m 檔案, 新增 method 如下:
- (IBAction)didEndEdit:(id)sender {
// 放棄成為第一回應者(鍵盤消失)
[sender resignFirstResponder];
}
3. 開啟 ManageNBViewController.xib 檔案:
將 File's Owner 裡的 didEndEdit: mehod 分別與 UI 的二個 Text Field 做連結.
----------------------------------------------------------------------------------------------------------------
- (IBAction)hitBackground:(id)sender;
2. 開啟 ManageNBViewController.m 檔案, 新增 method 如下:
- (IBAction)hitBackground:(id)sender {
//放棄成為第一回應者(鍵盤消失)
[nameField resignFirstResponder];
[subNameField resignFirstResponder];
}
3. 開啟 ManageNBViewController.xib 檔案:
(1). 將 UIView 的 Class 設定成 UIControl,
只有 UIControl Class 的控制項才可以觸發動作.
(2). 將 File's Owner 裡的 hitBackground: mehod
與剛剛的 UIControl 裡的 Touch Up Inside 作連結.
----------------------------------------------------------------------------------------------------------------
K. 結果圖:
1. 瀏覽首頁:
2. 新增 NoteBook 名稱:
3. 新增完成:
4. 刪除:
update: 2011/09/02
A. 新增 "新增與編輯" 的介面:
1. 專案目錄 > New File >
2. iOS > Cocoa Touch > UIViewController subclass > Next >
3. Subclass 選擇: UIViewController, 並勾選(預設): With XIB for user interface > Next
4. Save As: ManageNBViewController.m
----------------------------------------------------------------------------------------------------------------
B. 開啟 ManageNBViewController.h 修改如下:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@class NoteBookViewController; // 要當作 parenet Controller
@interface ManageNBViewController : UIViewController {
IBOutlet UITextField *nameField; // NoteBook 的名稱
IBOutlet UITextField *subNameField; // NoteBook 的副名稱
NoteBookViewController *parenetController; // A poniter to parent controller
NSManagedObject *noteBook; // represents the current note
}
@property (nonatomic, retain) IBOutlet UITextField *nameField;
@property (nonatomic, retain) IBOutlet UITextField *subNameField;
@property (nonatomic, retain) NoteBookViewController *parenetController;
@property (nonatomic, retain) NSManagedObject *noteBook;
- (id)initWithParentController:(NoteBookViewController *)aParentController noteBook:(NSManagedObject *)aNoteBook;
- (IBAction)save:(id)sender;
- (IBAction)cancel:(id)sender;
----------------------------------------------------------------------------------------------------------------
C. 開啟 ManageNBViewController.m 修改如下:
#import "ManageNBViewController.h"
#import "NoteBookViewController.h"
@synthesize nameField, subNameField, parenetController, noteBook;
- (id)initWithParentController:(NoteBookViewController *)aParentController noteBook:(NSManagedObject *)aNoteBook
{
if ((self = [super init])) {
self.parenetController = aParentController;
self.noteBook = aNoteBook;
}
return self;
}
- (IBAction)save:(id)sender {
if (parenetController != nil) {
/* editing */
if (noteBook != nil) {
[noteBook setValue:nameField.text forKey:@"name"];
[noteBook setValue:subNameField.text forKey:@"subName"];
//now date
[noteBook setValue:[NSDate date] forKey:@"dateUpdate"];
// Save the context
[parenetController saveContext];
}
/* creating a new noteBook */
else {
[parenetController insertNoteBookWithName:nameField.text subName:subNameField.text];
}
}
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)cancel:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if (noteBook != nil) {
nameField.text = [noteBook valueForKey:@"name"];
subNameField.text = [noteBook valueForKey:@"subName"];
}
else {
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.nameField = nil;
self.subNameField = nil;
}
- (void)dealloc {
[parenetController release];
[noteBook release];
[super dealloc];
}
----------------------------------------------------------------------------------------------------------------
D. 建立實際的 UI:
1. 拖拉一個 Navigation Bar 到 view 上, 對齊 view 最上方的中間,
將 title 改為: NoteBook.
2. 拖拉二個 Bar Button Item 到 Navigation Bar 上, 一個放在左邊, 一個放在右邊.
(1). 分別更改它們的 label, 左邊為: Canel, 右邊為: Save
(2). 並分別與 File's Owner 上的 cancel: 與 save: 作連結.
3. 拖拉一個 Label 到 view 的左上方, Navigation Bar 的下面, 將其 text 改為: Name:
4. 拖拉一個 TextField 到 Name: Label 的右方, 並與 File's Owner
上的 nameField 作連結.
5. 拖拉一個 Label 到 Name: Label 的下方, 將其 text 改為: SubName:
6. 拖拉一個 TextField 到 SubName: Label 的右方, 並與 File's Owner
上的 subNameField 作連結.
----------------------------------------------------------------------------------------------------------------
E. 開啟 NoteBookViewController.h 檔案, 新增二個 method 如下:
- (void)showManageNBView;
- (void)insertNoteBookWithName:(NSString *)name subName:(NSString *)subName;
----------------------------------------------------------------------------------------------------------------
F. 開啟 NoteBookViewController.m 檔案, 新增二個 method 如下:
#import "NoteBookViewController.h"
#import "ManageNBViewController.h"
- (void)showManageNBView {
ManageNBViewController *manageNBViewController = [[ManageNBViewController alloc]initWithParentController:self noteBook:nil]; // so a new noteBook will be created
[self presentModalViewController:manageNBViewController animated:YES];
[manageNBViewController release];
}
- (void)insertNoteBookWithName:(NSString *)name subName:(NSString *)subName {
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
NSManagedObject *newNoteBook = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
[newNoteBook setValue:name forKey:@"name"];
[newNoteBook setValue:subName forKey:@"subName"];
//Date
[newNoteBook setValue:[NSDate date] forKey:@"dateCreated"];
[newNoteBook setValue:[NSDate date] forKey:@"dateUpdate"];
// Save the context
[self saveContext];
}
----------------------------------------------------------------------------------------------------------------
G. 幫 NoteBook 介面增加 "新增" 與 "編輯" 的按鈕.
開啟 NoteBookViewController.m 檔案, 修改如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// 實際上是進入刪除的功能
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showManageNBView)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}
----------------------------------------------------------------------------------------------------------------
H. 增加: 編輯 NoteBook 功能:
開啟 NoteBookViewController.m 檔案, 修改如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *noteBook = [[self fetchedResultsController] objectAtIndexPath:indexPath];
ManageNBViewController *manageNBViewController = [[ManageNBViewController alloc] initWithParentController:self noteBook:noteBook];
[self presentModalViewController:manageNBViewController animated:YES];
[manageNBViewController release];
}
----------------------------------------------------------------------------------------------------------------
I. UITextField 輸入結束後, 收起小鍵盤.
1. 開啟 ManageNBViewController.h 檔案, 新增 method 如下:- (IBAction)didEndEdit:(id)sender;
2. 開啟 ManageNBViewController.m 檔案, 新增 method 如下:
- (IBAction)didEndEdit:(id)sender {
// 放棄成為第一回應者(鍵盤消失)
[sender resignFirstResponder];
}
3. 開啟 ManageNBViewController.xib 檔案:
將 File's Owner 裡的 didEndEdit: mehod 分別與 UI 的二個 Text Field 做連結.
----------------------------------------------------------------------------------------------------------------
J. 點擊 UITextField 輸入的背景收起小鍵盤.
1. 開啟 ManageNBViewController.h 檔案, 新增 method 如下:- (IBAction)hitBackground:(id)sender;
2. 開啟 ManageNBViewController.m 檔案, 新增 method 如下:
- (IBAction)hitBackground:(id)sender {
//放棄成為第一回應者(鍵盤消失)
[nameField resignFirstResponder];
[subNameField resignFirstResponder];
}
3. 開啟 ManageNBViewController.xib 檔案:
(1). 將 UIView 的 Class 設定成 UIControl,
只有 UIControl Class 的控制項才可以觸發動作.
(2). 將 File's Owner 裡的 hitBackground: mehod
與剛剛的 UIControl 裡的 Touch Up Inside 作連結.
----------------------------------------------------------------------------------------------------------------
K. 結果圖:
1. 瀏覽首頁:
標籤:
開發筆記,
iphone,
Lala's Program Note
訂閱:
文章 (Atom)











