iphone UITableView及UIWebView的使用
1。新建一个基于Navigation-based Application的工程。
2。修改原来的RootViewController.h,RootViewController.m,RootViewController.xib为MyTableViewController.h,MyTableViewController.m,MyTableViewController.xib。
3。点击MainVindow.xib,将Rot View Controller的class设置为:MyTableViewController。
4。新建文件:DetailViewController.m文件并选择自动生成.h与.xib文件,然后在DetailViewController.xib拖入一个map view控件。
5。代码:(控件与属性连接就省略了)
第一个页面的代码:
MyTableViewController.h
- #import <UIKit/UIKit.h>
- @interface MyTableViewController : UITableViewController {
- NSMutableArray *listData;//表格第一部分的数据
- NSMutableArray * twolistData;//表格第二部分的数据
- }
- @property(nonatomic,retain) NSMutableArray *listData;
- @property(nonatomic,retain) NSMutableArray *twolistData;
- @end
#import <UIKit/UIKit.h>
@interface MyTableViewController : UITableViewController {
NSMutableArray *listData;//表格第一部分的数据
NSMutableArray * twolistData;//表格第二部分的数据
}
@property(nonatomic,retain) NSMutableArray *listData;
@property(nonatomic,retain) NSMutableArray *twolistData;
@end
MyTableViewController.m
- #import "MyTableViewController.h"
- #import "DetailViewController.h"
- @implementation MyTableViewController
- @synthesize listData;
- @synthesize twolistData;
- #pragma mark -
- #pragma mark View lifecycle
- //设置标题和初始化表格数据
- - (void)viewDidLoad {
- [super viewDidLoad];
- listData = [[NSMutableArray alloc] initWithObjects:@"Beijing",@"Shanghai",@"Guangzhou",nil];
- twolistData = [[NSMutableArray alloc] initWithObjects:@"Changsha", nil];
- self.title = @"第一个页面";
- }
- #pragma mark -
- #pragma mark Table view data source
- // 设置表格为两个部分
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 2;
- }
- //设置每个部分的标题
- - (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 {
- if (section == 0) {
- return listData.count;
- }
- else if (section == 1) {
- return twolistData.count;
- }
- return 0;
- }
- // 设置行数据
- - (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];
- }
- if (indexPath.section == 0) {
- cell.textLabel.text = [listData objectAtIndex:indexPath.row];
- }
- else if(indexPath.section == 1){
- cell.textLabel.text = [twolistData objectAtIndex:indexPath.row];
- }
- return cell;
- }
- #pragma mark -
- #pragma mark Table view delegate
- //表格行点击事件
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- NSString *selectedRow;
- if (indexPath.section == 0) {
- selectedRow = [listData objectAtIndex:indexPath.row];
- }else if(indexPath.section == 1){
- selectedRow = [twolistData objectAtIndex:indexPath.row];
- }
- DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
- detailViewController.selectedRow = selectedRow;
- [self.navigationController pushViewController:detailViewController animated:YES];
- [detailViewController release];
- }
- #pragma mark -
- #pragma mark Memory management
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (void)viewDidUnload {
- }
- - (void)dealloc {
- [listData release];
- [twolistData release];
- [super dealloc];
- }
- @end
#import "MyTableViewController.h"
#import "DetailViewController.h"
@implementation MyTableViewController
@synthesize listData;
@synthesize twolistData;
#pragma mark -
#pragma mark View lifecycle
//设置标题和初始化表格数据
- (void)viewDidLoad {
[super viewDidLoad];
listData = [[NSMutableArray alloc] initWithObjects:@"Beijing",@"Shanghai",@"Guangzhou",nil];
twolistData = [[NSMutableArray alloc] initWithObjects:@"Changsha", nil];
self.title = @"第一个页面";
}
#pragma mark -
#pragma mark Table view data source
// 设置表格为两个部分
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
//设置每个部分的标题
- (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 {
if (section == 0) {
return listData.count;
}
else if (section == 1) {
return twolistData.count;
}
return 0;
}
// 设置行数据
- (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];
}
if (indexPath.section == 0) {
cell.textLabel.text = [listData objectAtIndex:indexPath.row];
}
else if(indexPath.section == 1){
cell.textLabel.text = [twolistData objectAtIndex:indexPath.row];
}
return cell;
}
#pragma mark -
#pragma mark Table view delegate
//表格行点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedRow;
if (indexPath.section == 0) {
selectedRow = [listData objectAtIndex:indexPath.row];
}else if(indexPath.section == 1){
selectedRow = [twolistData objectAtIndex:indexPath.row];
}
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailViewController.selectedRow = selectedRow;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[listData release];
[twolistData release];
[super dealloc];
}
@end
第二个页面的代码:
DetailViewController.h
- #import <UIKit/UIKit.h>
- #import <Foundation/Foundation.h>
- @interface DetailViewController : UIViewController {
- NSString *selectedRow;//用来保存前一个页面传过来的参数
- IBOutlet UIWebView *webView;//浏览器控件
- }
- @property (nonatomic,retain) NSString *selectedRow;
- @property (nonatomic,retain) UIWebView *webView;
- @end
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface DetailViewController : UIViewController {
NSString *selectedRow;//用来保存前一个页面传过来的参数
IBOutlet UIWebView *webView;//浏览器控件
}
@property (nonatomic,retain) NSString *selectedRow;
@property (nonatomic,retain) UIWebView *webView;
@end
DetailViewController.m
- #import "DetailViewController.h"
- @implementation DetailViewController
- @synthesize selectedRow,webView;
- //设置标题和根据传过来的参数打开google地图
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.title = selectedRow;//设置标题
- NSString* addressText = selectedRow;
- addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
- NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText];
- webView.userInteractionEnabled = true;
- [webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:urlText]]];//打开google地图
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (void)viewDidUnload {
- [super viewDidUnload];
- }
- - (void)dealloc {
- [super dealloc];
- [selectedRow release];
- [webView release];
- }
- @end
#import "DetailViewController.h"
@implementation DetailViewController
@synthesize selectedRow,webView;
//设置标题和根据传过来的参数打开google地图
- (void)viewDidLoad {
[super viewDidLoad];
self.title = selectedRow;//设置标题
NSString* addressText = selectedRow;
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText];
webView.userInteractionEnabled = true;
[webView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:urlText]]];//打开google地图
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
[selectedRow release];
[webView release];
}
@end
iphone UITableView及UIWebView的使用相关推荐
- [iPhone开发]UIWebview 嵌入 UITableview
UIWebview 嵌入UITableview, 将UIWebview 加入到UITableview的cell里面,而且,将UIWebview 的高度设为内容的高度, 内容的高度,可以当UIWebvi ...
- iPhone SDK开发基础之iPhone程序框架
总的来说iPhone程序有两类框架,一类是游戏框架,另一类是非游戏框架,这里介绍的是非游戏框架,即基于iPhone 用户界面标准控件的程序框架. 典型的iPhone程序包含一个Window和几个UIV ...
- 了解iPhone App特点及基本设计方法
了解iPhone App特点及基本设计方法 本文介绍的是了解iPhone App特点及基本设计方法,很详细的介绍了设计方法,我们先来看内容. AD: 本文主要是来了解iPhone App特点及基本设计 ...
- 《iOS 8开发指南(第2版)》——第1章,第1.3节工欲善其事,必先利其器——搭建开发环境...
本节书摘来自异步社区<iOS 8开发指南(第2版)>一书中的第1章,第1.1节1.3 工欲善其事,必先利其器--搭建开发环境,作者 管蕾,更多章节内容可以访问云栖社区"异步社区& ...
- UIScrollView点击StatusBar返回顶部失效的解决
查看UIScrollView的头文件可以发现这样一段注释: // When the user taps the status bar, the scroll view beneath the touc ...
- iOS crash 问题分析汇总
在这里插入图片描述 iOS crash 问题分析 iOS crash 常用分析工具 IOS 崩溃日志 iOS crash 原因分析 1.调用悬浮指针 2.数组越界访问 3.调用了未实现的方法 4.调用 ...
- iOS uiscrollView 嵌套 问题 的解决
苹果官方文档里面提过,最好不要嵌套scrollView,特别提过UITableView和UIWebView,因为在滑动时,无法知道到底是希望superScrollView滑动还是subScrollVi ...
- 在Scrollview中使用AutoLayout
AutoLayout 与 UIScrollView的相遇是一个不可避免的场景,像UITableView.UIWebView这些都是继承于UIScrollView的,关于它们的autolayout布局大 ...
- 将网页保存为webarchive文件的代码
来自网络https://github.com/takebayashi/STWebArchiver 用于Mac电脑上的代码,可以将指定URL的网页保存为.webarchive 文件,iPhone上的UI ...
最新文章
- oracle增加数据时报没安装java_在linux上安装Oracle Developer Tools for VS Code
- cocos2d-x学习笔记03:绘制基本图元
- 多列布局——column-width
- boost使用split分割字符串
- some example of SAP odata annotation in metadata
- winform中listview选中整行_工作中常见的11个Excel难题,一次解决!
- python编程狮app题库_Python编程狮-零基础学Python App Storessa
- 粒子滤波随机采样算法
- 下载超星或读秀图书时,怎么搞定完整书签?
- 轮廓(查找和绘制轮廓、轮廓的表达与组织、轮廓的特性)
- Nginx网页优化(隐藏版本号,日志分割,更改进程数,网页压缩,防盗链详
- 小数除法竖式计算过程
- 字节跳动布局游戏,打算从腾讯的碗里“抢饭吃”?
- 武汉坚守第二十二天——谣言与辟谣与慌乱
- 洛谷 P1361 小猫爬山
- django模型层FQ查询,only,defer关键字,orm简单事务
- matlab如何查看眼图q值,详解:什么是眼图、眼图怎么看?
- 4668. 【NOIP2016提高A组模拟7.19】腐败
- shell脚本一步完成多层ssh跳转时的文件传输:Multi-layer scp
- 响应式设计和移动端优化:如何实现页面在不同设备上的适配和优化
热门文章
- 【C++ grammar】Enhancement for Type System (C++11 对类型系统的增强)
- strictmath_Java StrictMath sqrt()方法与示例
- 兰州交通大学C语言课程设计,兰州交通大学C语言课程设计报告(完整版).doc
- [Golang]计算一个文件的MD5值
- net-tools和ifconfig
- PyCharm和git安装教程
- 网络基础3-1(细谈IP协议头, 网络层,子网划分,路由选择,数据链路层,以太网帧格式,MAC地址,再谈ARP协议)
- leetcode283.移动零
- Epoll 的tcp通信代码(服务器+客户端)
- Linux下多线程模拟停车场停车