最近做的也个项目,要做一个IOS的新闻展示view(有图有文字,不用UIwebview,因为数据是用webservice解析的到的json数据),自己一直没有头绪,可后来听一个学长说可以用listview.。但我查了查ios好像没有listview。于是就用UITableView和自定义cell解决了这个问题。

效果图如下:

UITableView:

  1. //
  2. //  NewsDetailViewController.h
  3. //  SildToDo
  4. //
  5. //  Created by WildCat on 13-8-18.
  6. //  Copyright (c) 2013年 wildcat. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. @interface NewsDetailViewController : UITableViewController
  10. @property (nonatomic,copy) NSArray *dataArray;
  11. @property (nonatomic, strong)
  12. UISwipeGestureRecognizer *swipeGestureRecognizer;
  13. @end
  1. //
  2. //  NewsDetailViewController.m
  3. //  SildToDo
  4. //
  5. //  Created by WildCat on 13-8-18.
  6. //  Copyright (c) 2013年 wildcat. All rights reserved.
  7. //
  8. #import "NewsDetailViewController.h"
  9. #import "MyTableViewImageCell.h"
  10. #define FONT_SIZE 14.0f
  11. #define TITLE_FONT_SIZE 18.0f
  12. #define CELL_CONTENT_WIDTH 320.0f
  13. #define CELL_CONTENT_MARGIN 12.0f
  14. @interface NewsDetailViewController ()
  15. @property NSInteger  lableCount;
  16. @end
  17. @implementation NewsDetailViewController
  18. @synthesize dataArray;
  19. @synthesize lableCount;
  20. @synthesize swipeGestureRecognizer;
  21. - (id)initWithStyle:(UITableViewStyle)style
  22. {
  23. self = [super initWithStyle:style];
  24. if (self) {
  25. }
  26. return self;
  27. }
  28. - (void)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. UIImage *myimage1=[UIImage imageNamed:@"3.jpeg"];
  32. UIImage *myimage2=[UIImage imageNamed:@"2.jpg"];
  33. self.dataArray=[NSArray arrayWithObjects:@"小米手机-HAXLR8on硬件黑客马拉松 开团了!",@" 2 由小米手机独家冠名,CSDN、Seeed Studio、HAXLR8R联合举办的硬件黑客马拉松“小米手机-HAXLR8on”,将于8月24日至8月25日在深圳贝塔咖啡举行。你希望做一个手机拍照的控制器?还是高端一点,做一个由脑波控制的小设备?这些在这里都能实现!本次比赛设有一等奖一个、二等奖两个、三等奖三个以及参与奖若干。一等奖获得者可赢取由小米独家提",myimage2,@" 3  供的10000元奖金和一部小米手机,而且每一位参赛者也都由小米手机独家冠名,CSDN、Seeed Studio、HAXLR8R联合举办的硬件黑客马拉松“小米",myimage1,@"  4  手机-HAXLR8on”,将于8月24日至8月25日在深圳贝塔咖啡举行。你希望做一个手机拍照的控制器?还是高端一点,做一个由脑波控制的小设备?这些在这里都能实现!本次比赛设有一等奖一个、二等奖两个、三等奖三个以及参与奖若干。一等奖获得者可赢取由小米独家提供的10000元奖金和一部小米手机,而且每一位参赛者也都将获得由小米独家提供小米盒子一台。", nil];
  34. //手势识别
  35. self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]
  36. initWithTarget:self action:@selector(handleSwipes:)];
  37. /* Swipes that are performed from right to left are to be detected */
  38. self.swipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionRight;
  39. self.swipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionDown;
  40. /* Just one finger needed */
  41. self.swipeGestureRecognizer.numberOfTouchesRequired = 1;
  42. /* Add it to the view */
  43. [self.tableView addGestureRecognizer:self.swipeGestureRecognizer];
  44. self.tableView.bounces=NO;
  45. }
  46. - (void)viewDidUnload
  47. {
  48. [super viewDidUnload];
  49. self.swipeGestureRecognizer = nil;
  50. }
  51. //手势识别处理方法
  52. - (void) handleSwipes:(UISwipeGestureRecognizer *)paramSender{
  53. if (paramSender.direction & UISwipeGestureRecognizerDirectionDown){ NSLog(@"Swiped Down.");
  54. }
  55. if (paramSender.direction & UISwipeGestureRecognizerDirectionLeft){
  56. NSLog(@"Swiped Left."); }
  57. if (paramSender.direction & UISwipeGestureRecognizerDirectionRight){ NSLog(@"Swiped Right.");
  58. }
  59. if (paramSender.direction & UISwipeGestureRecognizerDirectionUp){
  60. NSLog(@"Swiped Up."); }
  61. }
  62. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  63. {
  64. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  65. }
  66. #pragma mark - Table view data source
  67. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  68. {
  69. return 1;
  70. }
  71. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  72. {
  73. return [self.dataArray count];
  74. }
  75. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. static NSString *CellIdentifier = @"Cell";
  78. static NSString *ImageCellIdentifier = @"MyTableViewImageCell";
  79. UILabel *label = nil;
  80. UITableViewCell *cell=nil;
  81. MyTableViewImageCell *imageCell=nil;
  82. //判断对象的类型
  83. if ([[self.dataArray objectAtIndex:[indexPath row]] isKindOfClass:[NSString class]]) { //如果是文字
  84. cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  85. CGSize size;
  86. if (cell == nil)
  87. {
  88. cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  89. label = [[UILabel alloc] initWithFrame:CGRectZero];
  90. [label setLineBreakMode:UILineBreakModeWordWrap];
  91. [label setNumberOfLines:0];
  92. [label setTag:1];
  93. [[cell contentView] addSubview:label];
  94. }
  95. NSString *text = [self.dataArray objectAtIndex:[indexPath row]];
  96. if (!label){ label = (UILabel*)[cell viewWithTag:1];}
  97. CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
  98. if (indexPath.row==0) {  //如果是文章标题
  99. [label setFont:[UIFont  boldSystemFontOfSize:TITLE_FONT_SIZE]];
  100. [label setMinimumFontSize:TITLE_FONT_SIZE];
  101. size = [text sizeWithFont:[UIFont  boldSystemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
  102. }else{
  103. [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
  104. [label setMinimumFontSize:FONT_SIZE];
  105. size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
  106. }
  107. [label setText:text];
  108. [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
  109. }else if([[self.dataArray objectAtIndex:[indexPath row]] isKindOfClass:[UIImage class]]){ //如果是图片
  110. imageCell= [tableView dequeueReusableCellWithIdentifier:ImageCellIdentifier];
  111. if (cell==nil) {
  112. cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageCellIdentifier];
  113. }
  114. imageCell.myImageView.image=[self.dataArray objectAtIndex:[indexPath row]];
  115. imageCell.selectionStyle = UITableViewCellSelectionStyleNone;//不能被选择
  116. return imageCell;
  117. }
  118. cell.selectionStyle = UITableViewCellSelectionStyleNone;//不能被选择
  119. return  cell;
  120. }
  121. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  122. NSString *text;
  123. CGFloat lastHeight=0.f;
  124. if ([[self.dataArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]) {
  125. text = [self.dataArray objectAtIndex:indexPath.row];
  126. CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
  127. CGSize size;
  128. if (indexPath.row==0) {
  129. size = [text sizeWithFont:[UIFont boldSystemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
  130. }else{
  131. size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
  132. }
  133. CGFloat height = MAX(size.height, 44.0f);
  134. lastHeight=height + (CELL_CONTENT_MARGIN * 2);
  135. }else{
  136. if ([[self.dataArray objectAtIndex:indexPath.row] size].height>112.f) {
  137. lastHeight=112.f;
  138. }else{
  139. lastHeight=[[self.dataArray objectAtIndex:indexPath.row] size].height;
  140. }
  141. }
  142. return lastHeight;
  143. }
  144. #pragma mark - Table view delegate
  145. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  146. {
  147. }
  148. @end

UItableViewCell:

  1. //  MyTableViewImageCell.h
  2. //  SildToDo
  3. //
  4. //  Created by WildCat on 13-8-18.
  5. //  Copyright (c) 2013年 wildcat. All rights reserved.
  6. //
  7. #import <UIKit/UIKit.h>
  8. @interface MyTableViewImageCell : UITableViewCell
  9. @property (weak, nonatomic) IBOutlet UIImageView *myImageView;
  10. @end
  1. //  MyTableViewImageCell.m
  2. //  SildToDo
  3. //
  4. //  Created by WildCat on 13-8-18.
  5. //  Copyright (c) 2013年 wildcat. All rights reserved.
  6. //
  7. #import "MyTableViewImageCell.h"
  8. @implementation MyTableViewImageCell
  9. @synthesize myImageView;
  10. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  11. {
  12. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  13. if (self) {
  14. // Initialization code
  15. }
  16. return self;
  17. }
  18. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
  19. {
  20. [super setSelected:selected animated:animated];
  21. // Configure the view for the selected state
  22. }
  23. @end

StoryBoard:

具体操作我就不说了挺简单,想知道的可以到新浪微博@我。

新浪微博:http://weibo.com/u/3202802157

转载请注明:
本文转自:http://blog.csdn.net/wildcatlele

IOS学习之路十四(用TableView做的新闻客户端展示页面)的更多相关文章

  1. 用TableView做的新闻客户端展示页面

    用TableView做的新闻客户端展示页面 //  MyTableViewImageCell.m //  SildToDo // //  Created by WildCat on 13-8-18. ...

  2. IOS学习之路十二(UITableView下拉刷新页面)

    今天做了一个下拉刷新的demo,主要用到了实现的开源框架是:https://github.com/enormego/EGOTableViewPullRefresh 运行结果如下: 实现很简单下载源代码 ...

  3. iOS学习笔记(十四)——打电话、发短信

    电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...

  4. 学习之路十四:客户端调用WCF服务的几种方法小议

    最近项目中接触了一点WCF的知识,也就是怎么调用WCF服务,上网查了一些资料,很快就搞出来,可是不符合头的要求,主要有以下几个方面: ①WCF的地址会变动,地址虽变,但是里面的逻辑不变! ②不要引用W ...

  5. 我的iOS学习之路(四):动画设置

    在ios的开发过程中,经常需要对视图控件进行变化,如大小,颜色,旋转等,这是如果直接将变化结果呈现出来,就显得不够友好,所以我们通常会使用动画,让用户能够看到变化的过程. 使用动画通常有两种方式,一种 ...

  6. 嵌入式Linux驱动学习之路(十四)按键驱动-同步、互斥、阻塞

    目的:同一个时刻,只能有一个应用程序打开我们的驱动程序. ①原子操作: v = ATOMIC_INIT( i )  定义原子变量v并初始化为i atomic_read(v)        返回原子变量 ...

  7. zigbee学习之路(十四):基于协议栈的无线数据传输

    一.前言 上次实验,我们介绍了zigbee原理的应用与使用,进行了基于zigbee的串口发送协议,但是上个实验并没有实现数据的收发.在这个实验中,我们要进行zigbee的接受和发送实验. 二.实验功能 ...

  8. IOS学习之路十五(UIView 添加背景图片以及加边框)

    怎样给UIview添加背景图片呢很简单,就是先给view添加一个subview,然后设为背景图片: 效果图如下: 很简单直接上代码: //设置内容 self.myTopView.backgroundC ...

  9. IOS学习之路十(仿人人滑动菜单Slide-out Sidebar Menu)

    最近滑动菜单比较流行,像facebook和人人等都在使用滑动菜单,今天做了一个小demo大体效果如下: 这次用了一个开源的项目ECSlidingViewController这个也是一个挺著名的托管在G ...

随机推荐

  1. Openstack本学习笔记——Neutron-server服务加载和启动源代码分析(三)

    本文是在学习Openstack过程中整理和总结.因为时间和个人能力有限.错误之处在所难免,欢迎指正! 在Neutron-server服务载入与启动源代码分析(二)中搞定模块功能的扩展和载入.我们就回到 ...

  2. Cocos2d-android游戏引擎-介绍

    一.游戏引擎概念 什么是游戏引擎       游戏引擎是指一些已编写好的可编辑游戏系统或者一些交互式实时图像应用程序的核心组件.这些系统为游戏设计者提供各种编写游戏所需的各种工具,其目的在于让游戏设计 ...

  3. Java泛型解析(03):虚拟机运行泛型代码

    Java泛型解析(03):虚拟机运行泛型代码      Java虚拟机是不存在泛型类型对象的,全部的对象都属于普通类,甚至在泛型实现的早起版本号中,可以将使用泛型的程序编译为在1.0虚拟机上可以执行的 ...

  4. 互联网点对点通讯(P2P)

    摘自: http://www.cnblogs.com/zhili/archive/2012/09/14/p2p_pnpr.html 很遗憾,目前看了下代码,我测试只是做到了本地p2p. [C# 网络编 ...

  5. 菜鸟进阶Android Touch事件传递(四)

    尊重他人劳动成果,转载请说明出处:http://blog.csdn.net/bingospunky/article/details/44343477 在该系列文章第四篇.我准备介绍一下viewpage ...

  6. centos下mysql 最新版最终成功安装!备份一下几个关键地方

    我本来仅仅是为了搭建简单的LAMP环境,亲自己主动手,却发现有这么多的问题会发生.(by default7#zbphp.com) 非常多地方给的安装Mysql的提示是通过yum一键安装.shell命令 ...

  7. Premiere Pro CC问题集,不断更新

    1.Premiere Pro CC不好用? 是的.原因如下: 1.1 Adobe公司不注重用户体验,不注重工作流程,导致这款软件的用户体验很差,设计也很烂.对比Adobe公司当年用户体验最好的软件 F ...

  8. 一个极简的守护进程Bash脚本

    由于最近写的Node.js程序因为一些Bug,会出现一些自动退出的问题,所以需要在它退出的时候及时发现,并重新启动 于是查阅了些资料,写了一个Bash的程序,功能十分简单,就是每隔3s判断一次处在60 ...

  9. C#:判断当前程序是否通过管理员运行

    原文:C#:判断当前程序是否通过管理员运行 public bool IsAdministrator() { WindowsIdentity current = WindowsIdentity.GetC ...

  10. at System.Data.EntityClient.EntityConnection.GetFactory(String providerString)

    最近在做一个WinForm的项目. 使用vs2013开发. 数据库使用的是oracle. 在本地写了一个webservice .测试正常.发布到服务器的时候.就是提示了错误. 打开服务器上的日志.看到 ...