日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iphone UITableView及UIWebView的使用

發布時間:2023/11/30 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

[cpp] view plaincopyprint?
  • #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

    [cpp] view plaincopyprint?
  • #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

    [cpp] view plaincopyprint?
  • #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

    [cpp] view plaincopyprint?
  • #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的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。