这一章主要是连续上一张所讲的基本操作,加入了单击单元格触发的操作- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (void)viewDidLoad {
[super viewDidLoad];
listData = [[NSMutableArray alloc] initWithObjects:@"Beijing",@"Shanghai",@"Guangzhou",nil];
self.title = @"第 16 天 地址收藏器";
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString* secHeader = @"";
if (section == 0)
{
secHeader = @"中国三大城市";
}
else if (section == 1)
{
secHeader = @"各国大城市";
}
return secHeader;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return listData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [listData objectAtIndex:indexPath.row];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
//单击单元格触发的操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//获取用户选择行数的对应的可修改数组listData位置相同的数据
NSString *selectedRow = [listData objectAtIndex:indexPath.row];
//创建链接详细页面的变量对象
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
//把所选的那一行数据selectedRow发送到detailViewController的selectedRow中
detailViewController.selectedRow = selectedRow;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
} 这一部分在前面第六天讲过的
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = selectedRow;
//创建一个字符变量addressText
NSString* addressText = selectedRow;
//在字符串程序的数据中把字符转换为url网页格式,NSASCIIStringEncoding中使用ASCII为字符串的格式转化
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
//addressText是对网站发送一个指定的地址
NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText];
//开启网页视图与用户交互属性值
webView.userInteractionEnabled = true;
//网页视图向网站发送一个指定网站内容的urlText数据变量
[webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:urlText]]];
}最后的效果图:

本文介绍了一个简单的iOS应用——地址收藏器的实现过程。该应用使用UITableView展示数据,并通过单击单元格触发显示地图的功能。文章详细展示了如何设置UITableView的数据源、代理方法以及如何将选中的城市名称传递给详情页面并在WebView中加载对应城市的Google地图。

被折叠的 条评论
为什么被折叠?



