如何在TableView-iOS上添加UIView

时间:2013-05-25 17:52:31

标签: c++ ios uitableview uiwebview

我正在开发一个RSS阅读器应用程序,我试图让应用程序只有2个视图(tableview和webview)我不知道如何在我的tableview上添加UIView

1 个答案:

答案 0 :(得分:0)

因为,我无法理解你的问题,希望一个人可以成为你的解决方案。

1。解决方案一,在同一页面上显示内容

enter image description here

一个。为此,在界面构建器文件中,拖动UIWebView并将其作为subView添加到View。 保持此webview顶部对齐。

湾现在拖动UITableView并将其作为子视图添加到UIView,例如y = 150并保持此tableview底部对齐。

℃。开始连接到您的代码文件的接口文件。首先,您需要在.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,您将看到datasource和delegate,将它们指向您的viewController。与它一起通过将其作为“引用插座”连接来制作feedTableView。

还将feedsWebView连接为“引用插座”。

d。完成连接后,我们就可以开始编写代码了。让我们用url初始化表格,点击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 *cellIdentifier=@"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只显示feed url及其相关细节。

然后你需要创建另一个UIViewController,它只有一个UIWebView,当选择了UITableViewCell时,它将被推送到UINavigationController上。

3. 第三种解决方案是创建customCell。该单元格将UIWebView作为子视图之一。

对于此初始,您可以将单元格的高度保持为44.0,但只要选择此单元格,就可以将高度增加到200,这样您的UIWebView就可见了。当点击发生时,在UIWebView上调用loadRequest。

要了解的事情当您在应用程序中使用UIWwebView时,您必须自己处理导航(后退和前进)。想一想就是使用以下代码打开ios Default浏览器。

-(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默认浏览器一样来回处理导航。