ディスクロージャボタンの表示
オプションで、図15に示す詳細ディスクロージャボタンなどの追加ボタンをTable Viewの行に表示することもできます。

ディスクロージャボタンを表示するには、tableView:cellForRowAtIndexPath:メソッドでUITableViewオブジェクトのaccessoryTypeプロパティを設定する必要があります。このメソッドに次のコードを追加します。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
//...
UIImage *image = [[UIImage imageNamed:@"USA.jpeg"]
_imageScaledToSize:CGSizeMake(30.0f, 32.0f)
interpolationQuality:1];
cell.imageView.image = image;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
詳細ディスクロージャボタンには2つのクリック可能領域があります。行と、ボタン自体です。ユーザーが行をクリックしたときは、前の例と同様にtableView:didSelectRowAtIndexPath:メソッドがトリガされます。しかし、詳細ディスクロージャボタンをクリックしたときは、代わりにtableView:accessoryButtonTappedForRowWithIndexPath:メソッドがトリガされます。従って、tableView:didSelectRowAtIndexPath:メソッドを次のように変更する必要があります。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
NSString *stateSelected = [listOfStates objectAtIndex:
[indexPath row]];
NSString *msg = [[NSString alloc] initWithFormat:
@"You have selected %@", stateSelected];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"State selected"
message:msg
delegate:self
cancelButtonTitle: @"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
また、次のようにtableView:accessoryButtonTappedForRowWithIndexPath:メソッドを実装する必要があります。
- (void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSString *stateSelected = [listOfStates objectAtIndex:
[indexPath row]];
NSString *msg = [[NSString alloc] initWithFormat:
@"You have selected %@",
stateSelected];
//---Navigate to the details view---
if (self.detailsViewController == nil)
{
DetailsViewController *d = [[DetailsViewController alloc]
initWithNibName:@"DetailsViewController"
bundle:[NSBundle mainBundle]];
self.detailsViewController = d;
[d release];
}
//---set the state selected in the method of the
// DetailsViewController---//
[self.detailsViewController initWithTextSelected:msg];
[self.navigationController
pushViewController:self.detailsViewController
animated:YES];
[msg release];
}
command+Rキーを押してアプリケーションをテストします。今度は、Table Viewの行をクリックするとアラートビューが表示され、詳細ディスクロージャボタンをクリックすると他方のビューウィンドウに移動します。
この記事では、iPhone SDKでTable Viewを作成し、項目を追加し、イベントを処理するための基本的な操作を学んできました。Table Viewは非常に用途の広いビューのため、その仕組みをよく理解することが重要です。このiPhone開発シリーズの次の記事では、さらにTable Viewでのテクニックをいくつか紹介します。
