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

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。