2011年11月30日 星期三

Lala's Program Note 實作記錄: 32. viewWillAppear: method 的相關處理

since: 2011/11/30
update: 2011/12/05

A. 當切回到 Favorite 或 Search Tab 時,  若文章已不存在, 則回到 navigationController
   的上一層, 不然就更新 Favorite 的點選狀態.


   => 修改  ArticleViewController.m 檔案如下:
//@add
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   
    NSLog(@"call ArticleViewController's viewWillAppear!");
   
    if (![article valueForKey:@"title"]) {
        NSLog(@"article deleted");
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self toggleFavoriteImage:[[article valueForKey:@"favorite"] boolValue]];
    }
}


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


B.
當切回到 Search Tab 時, 清除已不存在的 Search Result.

   => 修改  SearchViewController.m 檔案如下:

//@add
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"SearchCell";
   
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
   
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] autorelease];
    }
   
    // customize cell
    NoteArticle *noteArticle = [self.fetchedResultsController objectAtIndexPath:indexPath];
   
    //@add
    if (!noteArticle.title) {
        NSLog(@"SearchViewController's noteArticle removed");
        [self searchBarSearchButtonClicked:self.mySearchBar];
       
        return [[[UITableViewCell alloc] init] autorelease];
    }
    else
    {
        //@update
        NSMutableString *cellTitle = [[[NSMutableString alloc] init] autorelease];
        [cellTitle appendString:noteArticle.title];
        cell.textLabel.text = cellTitle;
        cell.textLabel.textColor = [UIColor brownColor];
       
        //@update
        NSMutableString *cellSubTitle = [[[NSMutableString alloc] init] autorelease];
        [cellSubTitle appendString:noteArticle.subTitle];
        cell.detailTextLabel.text = cellSubTitle;
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
       
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *article = [self.fetchedResultsController objectAtIndexPath:indexPath];
   
    //@add
    if (![article valueForKey:@"title"]) {
        NSLog(@"==> SearchViewController's noteArticle removed");
    }
    else {          
        ArticleViewController *articleViewController = [[ArticleViewController alloc] init];
        articleViewController.article = article;
       
        //@relationship
        articleViewController.title = [[article valueForKey:@"notebook"] valueForKey:@"name"];
       
        //@add
        articleViewController.managedObjectContext = self.managedObjectContext;
       
        [self.navigationController pushViewController:articleViewController animated:YES]; 
       
        [articleViewController release];
    }
}

//@add
- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"call SearchViewController's viewWillAppear!");
    [super viewWillAppear:animated];
   
    [myTableView reloadData];
}

沒有留言:

張貼留言

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