2011年9月10日 星期六

Lala's Program Note 實作記錄: 18. 切換至文章顯示畫面


since: 2011/09/10
update: 2011/12/02

A. 說明:
   由於已將在 TableView 上輕拍(Tap) 儲存格(Cell), 作成編輯的功能,
   因此改利用觸碰(touch) Cell 的 accessoryView 來觸發畫面的切換.

----------------------------------------------------------------------------------------------------------

B. 新增要切換的畫面:
  1. 專案目錄 > New File >
   2. iOS > Cocoa Touch > UIViewController subclass > Next >
   3. Subclass 選擇: UIViewController, 並勾選(預設): With XIB for user interface
      > Next
   4. Save As: ArticleViewController.m

----------------------------------------------------------------------------------------------------------

C. 在 ArticleListViewController.h 裡, 宣告以下的 method:
- (void)arrowBtnClicked:(id)sender event:(id)event;
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

----------------------------------------------------------------------------------------------------------

D. 在 ArticleListViewController.m 裡, 定義以下的 method:
#import "ArticleViewController.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 {

    ArticleViewController *articleViewController = [[ArticleViewController alloc] init];
   
    NSManagedObject *article = [[self fetchedResultsController] objectAtIndexPath:indexPath];

    articleViewController.title = [NSString stringWithFormat:@"%@",[article valueForKey:@"title"]];
   
    [self.navigationController pushViewController:articleViewController animated:YES];

    [articleViewController release];
}

----------------------------------------------------------------------------------------------------------

E. 在 ArticleListViewController.m 裡, 修改以下的 method:

- (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];
        //@update for Subtitle
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
       
        //@add
        //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        UIImage *image = [UIImage imageNamed:@"iArrowRight2-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...
    //@add
    // * need to implement the configureCell:atIndexPath: method
    [self configureCell:cell atIndexPath:indexPath];
   
    return cell;
}

沒有留言:

張貼留言

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