如何实现新闻详情页面
来源:互联网 发布:淘宝和拍拍网的区别 编辑:程序博客网 时间: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];}
- 如何实现新闻详情页面
- Fragment实现左右新闻列表和详情
- 网易新闻详情页排版实现思路
- Bannear实现一个详情页面
- RXjava封装实现详情页面
- 网易新闻IOS客户端文章详情页面的分析
- (八)-实现新闻页面
- jQuery实现页面详情展开收起
- MVP+Retrofit封装实现详情页面
- 网易新闻客户端iOS版本中新闻详情页(UIWebView)技术实现的分析探讨
- 网易新闻客户端iOS版本中新闻详情页(UIWebView)技术实现的分析探讨
- 网易新闻客户端iOS版本中新闻详情页(UIWebView)技术实现的分析探讨
- 网易新闻客户端iOS版本中新闻详情页(UIWebView)技术实现的分析探讨
- 新闻详情页,scrollview view label 等控件的使用,以及页面布局
- 蚂蚁分类信息系统5.8 解决手机端新闻详情页面图片不自动缩放问题
- 如何实现新闻自动抓取?
- 新闻客户端07 - 新闻详情页
- 170.n1-新闻详情listView展示新闻
- 线程等待与线程的返回值
- Android,使用activity与fragment的小结与使用的细节
- 小三角如何实现--css-before-after
- htons
- android 刷新媒体库(版本判断)
- 如何实现新闻详情页面
- python之open()函数
- 学习
- Myeclipse中package,source folder,folder区别
- 7 种将字符串反转的 Java 方法
- Objective-C MD5加密方法
- 浅谈Spring AOP
- tableview分割线左边长短的控制
- unity3d UGUI下拉框