Table Viewの勉強03〜セル(行)を選択したらSafariに飛ばす
ゴール
下記「セクション0 行0」をタップすると、Safariで決められたページを開くこと。
手順
行を選択したときに呼ばれる関数didSelectRowAtIndexPath内に、openURL処理を追加する。
ソース
.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if( indexPath.section == 0 ) { // セクション0の if( indexPath.row == 0 ) { // 0行目が選択されたら NSURL *url = [NSURL URLWithString:@"http://d.hatena.ne.jp/yamekodev/"]; // Safariで指定したURLを開く [[UIApplication sharedApplication] openURL:url]; } } }
ポイント
openURLという関数。これを呼ぶとSafariで指定したページを開ける。
TableViewにかぎらず有用。
次回以降
行を選択すると別のviewを開く処理あたり。