2013-05-25 74 views
-2

我正在開發一個RSS閱讀器應用程序,並試圖讓應用程序只有2個視圖(tableview和webview)我不知道如何添加一個UIView在我的tableview上。如何在TableView上添加UIView-iOS

+0

你真的需要給我們更多的信息。你試過什麼了?你是如何添加UITableView的?你有沒有試圖用你的UIWebView做同樣的事情? –

回答

0

因爲,我無法理解你的問題,希望能成爲你的解決方案。

解決方案之一,顯示在同一頁面

enter image description here

一個東西。爲此,在您的界面生成器文件中,拖動UIWebView並將其作爲子視圖添加到視圖。 保持此webview頂部對齊。

b。現在拖動一個UITableView並將它作爲子視圖添加到一個UIView,並在y = 150處保持這個tableview的底部對齊。

c。開始建立連接以將文件連接到您的代碼文件。首先你需要在.h頭文件中聲明它,如下所示。

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate> 
{ 
    NSMutableArray *feedsURLArray; 
} 
@property (weak,nonatomic) IBOutlet UITableView *feedsTableView; 
@property (weak,nonatomic) IBOutlet UIWebView *feedsWebView; 
@end 

聲明它後,你去接口生成器文件。現在右鍵單擊你的tableView,你會看到數據源和委託,將它們指向你的viewController。連同它通過將它連接爲「引用出口」來製作feedsTableView。

還將feedsWebView作爲「引用出口」連接。 d)。在完成連接後,我們可以開始編寫代碼。讓我們用urls初始化表格,點擊一個UITableViewCell,我們在UIWebView對象上創建一個NSURLRequest。這將寫入您的.m文件。

@implementation ViewController 
@synthesize feedsTableView,feedsWebView; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    feedsURLArray=[[NSMutableArray alloc] init]; 
    [feedsURLArray addObject:@"https://www.facebook.com"]; 
    [feedsURLArray addObject:@"http://www.yahoo.com"]; 
    [feedsURLArray addObject:@"http://www.google.com"]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark Table Methods 
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return feedsURLArray.count; 
} 


-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *[email protected]"Cell"; 

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 


    if(cell==nil){ 
     cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 
    cell.textLabel.text=[feedsURLArray objectAtIndex:indexPath.row]; 
    return cell; 
} 


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *urlStr=[feedsURLArray objectAtIndex:indexPath.row]; 
    [self.feedsWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]]; 
} 

@end 

2.二的解決辦法是您可以將您的UIViewController添加到一個UINavigationController。這個UIViewController將只顯示提要網址及其相關細節。

然後,您需要創建另一個UIViewController,它將只有一個UIWebView,當選擇UITableViewCell時,該UIWebView將在UINavigationController上推送。

3.第三個解決方案是創建一個customCell。這個單元格將有一個UIWebView作爲子視圖之一。

爲此,最初可以將單元格的高度保持爲44.0,但只要選中此單元格,就可以將高度增加爲200,以便您的UIWebView可見。發生敲擊時在UIWebView上調用loadRequest。

事情要知道當你在你的應用程序中使用UIWwebView時,你將不得不自己處理導航(後退和前進)。一個想法是使用下面的代碼打開ios默認瀏覽器。

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *urlStr=[feedsURLArray objectAtIndex:indexPath.row]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]]; 
} 

否則請參閱本網站Demo on creating a browser它處理來回航行,就像iOS的默認瀏覽器。

相關問題