UITableView、UIWebView的一点总结

前个星期,完成了老师布置的一个小项目,是通过解析xml文件来读取新浪新闻。

程序主要用到UINavigationController、UITabbarController、UITableViewController以及UIWebViewController。其中填在UINavigationController的目的呢,主要是可以让他为我产生一个标题栏,在以后需要对程序进行扩展的时候,也比较方便,之后主要的界面就是Tabbar里面嵌入TableView。因为我要读取四类新闻,所以就用一个UITabbarController,其中呢,每一个item都指向一个UITableViewController。之后四个UITableViewController的Cell都指向同一个UIWebView.

这就是我这个程序界面的构成了。storyboard的图片下一次补上,宿舍没有iMac

下面讲讲程序的一些主要的代码:

因为这个程序的主体是UITableViewController,所以重点是讲它,我挑显示体育新闻的这个TableView来说,首先当然需要创建一个类来关联这个UITableViewController,自己创建的这个类,要继承于UITableViewController,接下来,很重要的一点是要在storyboard中,选中UItableViewController的Cell,然后给它的Identifier一个自己定义的标识。UITableView中有2个重要的DataSource的函数,

#pragma mark - sportNewsTableViewControllerDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return ([self.newsPubdate count]-2);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"sportsNewsCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell...
    NSString *title = [self.newsTitle objectAtIndex:indexPath.row+2];
	NSString *pubdate = [self.newsPubdate objectAtIndex:indexPath.row+1];
    cell.textLabel.text = title;
    cell.detailTextLabel.text = pubdate;
    return cell;
}

第一个是返回用多少行,第二个是设置每一行。

还有一个函数是返回有多少section,因为默认的是1,而我的程序也只需要是1就可以了,所以那个函数我就直接删掉了,没实现。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

中,重要的参数就是indexPath,该参数有1个重要的property ,row 就是当前操作的是那一行,通过这个参数,我们才可以设置相应每一行的数据。

UITableView的delegate还有一个重要的函数

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

就是当某一行被选中时,需要做一些什么响应,因为我这次的程序是用segue,所以这个函数我不需要。

下面就说说segue,我是通过当某一行被选中的时,就转到一个UIWebView,去显示新闻的详细内容。因为我需要传一些参数过去给UIWebView,所以需要在我的UITableViewController中实现下面这个函数

#pragma mark - segue

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //do someting for the news content
    if([segue.identifier isEqualToString:@"sportNews"])
    {
        NSMutableString *str = [[NSMutableString alloc] init];
        [str appendString:[self.newsLink objectAtIndex:self.tableView.indexPathForSelectedRow.row+2]];
        [str deleteCharactersInRange:NSMakeRange(0, 4)];
        [segue.destinationViewController setWebURL:str];
        [segue.destinationViewController setNewsTitle:@"体育新闻"];
    }
}
这个又有一个很重要的细节,就是在创建完一个segue之后,同样要给它命名。

在这个函数里面,我主要是穿了被选中的那一个新闻的链接以及新闻的标题过去给UIWebView,所以在UIWebViewController中,也需要有相应的属性来装。

//
//  newsWebViewController.h
//  myNewsApplication
//
//  Created by mac11 on 12-3-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface newsWebViewController : UIViewController 
@property (nonatomic,strong) NSString *webURL;
@property (nonatomic,strong) NSString *newsTitle;
@property (nonatomic,weak) IBOutlet UIWebView *webView;
@end

//
//  newsWebViewController.m
//  myNewsApplication
//
//  Created by mac11 on 12-3-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "newsWebViewController.h"

@implementation newsWebViewController

@synthesize webView = _webView;
@synthesize webURL = _webURL;
@synthesize newsTitle = _newsTitle;

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.webView.scalesPageToFit = YES;
    NSString *newWebURL = [NSString stringWithString:self.webURL];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:newWebURL]]];
    self.navigationItem.title = self.newsTitle;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)viewDidUnload {
    [super viewDidUnload];
}
@end

上面就是UIWebViewController的文件的代码,很简单的代码,就是显示一个网页。

需要注意的一个细节是,我是将一个webView直接拖到一个ViewController上的,所以需要创建IBOutlet来关联webView.

好了,说了这么多,程序最重要的地方还没说,就是怎么获取新浪新闻的内容。

这个内容请看下一篇文章NSXMLParser解析xml文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值