如何实现新闻详情页面

来源:互联网 发布:淘宝和拍拍网的区别 编辑:程序博客网 时间:2024/05/20 09:46

首先我们先在XIB文件中添加一个UIScroolView 然后在UIScroolView中添加    两个UILabel 控件和一个UITextView控件

结构如下图所示

然后是配置.H文件

不多说。直接上代码比较直观

复制代码
#import <UIKit/UIKit.h>@interface SingleNewsInfoViewController : UIViewController{    NSString *mynewid;    UILabel *titlelabel;    UILabel *timelabel;    NSArray *images;    UITextView *contenttextview;    UIScrollView *mainscrollview;    BOOL haveimage;}@property(nonatomic,retain) IBOutlet UILabel *titlelabel;@property(nonatomic,retain) IBOutlet UILabel *timelabel;@property(nonatomic,retain) IBOutlet UITextView *contenttextview;@property(nonatomic,retain)IBOutlet UIScrollView *mainscrollview;@property(nonatomic,retain) NSString *mynewid;@property(nonatomic,retain) NSArray *images;@property(nonatomic) BOOL haveimage;@end
复制代码

然后在.m文件中声明好各个属性后 将对应的XIB文件中的控件与代码中的变量 建立关联

这边需要解释解释一下几个属性的用处

mynewid的话是由上一级页面 传递过来的新闻ID,我们可以通过该ID从后台获取新闻的详细参数

images的话是用来存储后来获取过来的图片集合。

建立好管理后我们再接着修改.M文件

.m文件的话还需要添加两个头文件

#import "GetWebInfo.h"

#import "Base64AndImageHelp.h"

GetWebInfo的话相信大家都已经知道是什么用的了。如果不知道的话请看前几张,

而Base64AndImageHelp类的话是我自定义的义个类,用来实现将base64的数据转化为Image格式的数据用的(这个会在之后的章节进行介绍)

对于.M文件的话我们只需要修改他的viewDidLoad方法就可以了

具体代码如下

代码相对而言没有什么特别难的地方

唯一有问题的可能就是Base64AndImageHelp部分,不过这部分 大家只要知道他是将后台传递过来的base64的string 图片数据转换成image数据就可以了

复制代码
- (void)viewDidLoad{    GetWebInfo *getwebinfo=[GetWebInfo alloc];    NSString *myparameters=[[NSString alloc] initWithString:[NSString stringWithFormat:@"Method=getSingleNewsbyId&new_id=%@",mynewid]];    getwebinfo.parameters=myparameters;    NSString *webReturnMessage=[getwebinfo dogetWebInfo];    NSData* jsonData=[webReturnMessage dataUsingEncoding:NSUTF8StringEncoding];    NSArray *keys =   [NSJSONSerialization                       JSONObjectWithData:jsonData                       options:NSJSONReadingMutableContainers                       error:nil];    //  NSLog(@"%@",keys);    timelabel.text=[[keys objectAtIndex:0]valueForKey:@"time"];    titlelabel.text=[[keys objectAtIndex:0 ]valueForKey:@"title"];    contenttextview.text=[[keys objectAtIndex:0 ]valueForKey:@"contents"];      NSInteger i=0;    if(haveimage)    {        images=[[[keys objectAtIndex:0 ]valueForKey:@"images"] componentsSeparatedByString:@","];        for (NSString *singleimage in images) {            NSData *newimage=[Base64AndImageHelp mydataWithBase64EncodedString:singleimage];            UIImage *newjiaban=[[UIImage alloc] initWithData:newimage];            UIImageView *imageView = [[UIImageView alloc] init];            [self.mainscrollview addSubview:imageView];            imageView.frame = CGRectMake(20 ,170*i+100,280,150);//left ,top ,width ,height            imageView.image=newjiaban;            i++;        }    }    contenttextview.frame=CGRectMake(20 ,170*i+100,280,310);    mainscrollview.contentSize=CGSizeMake(280, 390+170*i);    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.}
复制代码

到了这一步我们已经基本完成了新闻详细信息的浏览了。

最后我们还需要返回到NEWSViewController中 实现UITableView的行点击事件didSelectRowAtIndexPath,

实现点击了新闻标题后,跳转到新闻详细信息页面的效果。

首先需要在NEWSViewController添加SingleNewsInfoViewController的引用。

然后实现didSelectRowAtIndexPath方法,具体代码如下。

复制代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    SingleNewsInfoViewController *singlenewsinfoViewController= [[SingleNewsInfoViewController alloc]initWithNibName:@"SingleNewsInfoViewController" bundle:nil];    NSString *myid=[[NSString alloc] initWithFormat:@"%@",[[[_list objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] valueForKey:@"ID"]];    singlenewsinfoViewController.mynewid=myid;      NSString *haveImage=[[NSString alloc] initWithString:[[[_list objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]] valueForKey:@"images"]];    if([haveImage isEqual:@"False"])    {        singlenewsinfoViewController.haveimage=NO;    }    else    {        singlenewsinfoViewController.haveimage=YES;    }    singlenewsinfoViewController.title=@"新闻详情";        singlenewsinfoViewController.hidesBottomBarWhenPushed=YES;    [self.navigationController pushViewController:singlenewsinfoViewController animated:YES];}
0 0
原创粉丝点击