2 // ScanViewController.m
5 // Created by Christian Brunschen on 24/06/2008.
6 // Copyright 2008 Google Inc. All rights reserved.
9 #import "ScanViewController.h"
10 #import "ResultAction.h"
13 #define TEXT_VIEW_TAG 0x17
14 #define TITLE_HEIGHT 60
15 #define BODY_HEIGHT 100
17 @implementation ScanViewController
22 - (id)initWithResult:(ParsedResult *)r forScan:(Scan *)s {
23 if (self = [super initWithStyle:UITableViewStyleGrouped]) {
26 self.title = NSLocalizedString(@"Scan", @"scan view controller title");
32 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
33 return [[result actions] count] ? 2 : 1;
37 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
42 return [[result actions] count];
48 - (UITableViewCell *)cellWithIdentifier:(NSString *)identifier inTableView:(UITableView *)tableView {
49 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
51 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
56 - (UITableViewCell *)titleCellInTableView:(UITableView *)tableView {
57 static NSString *TitleIdentifier = @"ScanViewTitleIdentifier";
58 return [self cellWithIdentifier:TitleIdentifier inTableView:tableView];
61 - (UITableViewCell *)bodyCellInTableView:(UITableView *)tableView {
62 static NSString *BodyIdentifier = @"ScanViewBodyIdentifier";
63 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BodyIdentifier];
65 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, BODY_HEIGHT) reuseIdentifier:BodyIdentifier] autorelease];
66 UITextView *textView = [[UITextView alloc] initWithFrame:CGRectInset(cell.contentView.bounds, 6, 6)];
67 [textView setTag:TEXT_VIEW_TAG];
68 textView.editable = NO;
69 [textView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
70 [cell.contentView addSubview:textView];
76 - (UITableViewCell *)buttonCellInTableView:(UITableView *)tableView {
77 static NSString *ButtonIdentifier = @"ScanViewButtonIdentifier";
78 return [self cellWithIdentifier:ButtonIdentifier inTableView:tableView];
81 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
82 if (indexPath.section == 0) {
83 if (indexPath.row == 0) {
85 } else if (indexPath.row == 1) {
89 return tableView.rowHeight;
92 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
94 UITableViewCell *cell;
96 if (indexPath.section == 0) {
97 if (indexPath.row == 0) {
98 cell = [self titleCellInTableView:tableView];
99 cell.image = [result icon];
100 cell.text = [[result class] typeName];
101 } else if (indexPath.row == 1) {
102 cell = [self bodyCellInTableView:tableView];
103 UITextView *textView = (UITextView *)[cell viewWithTag:TEXT_VIEW_TAG];
104 textView.text = [result stringForDisplay];
106 } else if (indexPath.section == 1) {
107 cell = [self buttonCellInTableView:tableView];
108 ResultAction *action = [[result actions] objectAtIndex:indexPath.row];
109 cell.text = [action title];
115 - (void)performAction:(ResultAction *)action {
116 [action performActionWithController:self shouldConfirm:NO];
119 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
120 if (indexPath.section == 1) {
121 ResultAction *action = [[result actions] objectAtIndex:indexPath.row];
122 [self performSelector:@selector(performAction:) withObject:action afterDelay:0.0];
127 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
129 if (editingStyle == UITableViewCellEditingStyleDelete) {
131 if (editingStyle == UITableViewCellEditingStyleInsert) {
136 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
141 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
145 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
149 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
150 if (indexPath.section != 1) {
164 - (void)viewDidLoad {
169 - (void)viewWillAppear:(BOOL)animated {
170 [super viewWillAppear:animated];
173 - (void)viewDidAppear:(BOOL)animated {
174 [super viewDidAppear:animated];
177 - (void)viewWillDisappear:(BOOL)animated {
180 - (void)viewDidDisappear:(BOOL)animated {
183 - (void)didReceiveMemoryWarning {
184 [super didReceiveMemoryWarning];