IOS学习之路十四(用TableView做的新闻客户端展示页面)
最近做的也个项目,要做一个IOS的新闻展示view(有图有文字,不用UIwebview,因为数据是用webservice解析的到的json数据),自己一直没有头绪,可后来听一个学长说可以用listview.。但我查了查ios好像没有listview。于是就用UITableView和自定义cell解决了这个问题。
效果图如下:
UITableView:
- //
- // NewsDetailViewController.h
- // SildToDo
- //
- // Created by WildCat on 13-8-18.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface NewsDetailViewController : UITableViewController
- @property (nonatomic,copy) NSArray *dataArray;
- @property (nonatomic, strong)
- UISwipeGestureRecognizer *swipeGestureRecognizer;
- @end
- //
- // NewsDetailViewController.m
- // SildToDo
- //
- // Created by WildCat on 13-8-18.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import "NewsDetailViewController.h"
- #import "MyTableViewImageCell.h"
- #define FONT_SIZE 14.0f
- #define TITLE_FONT_SIZE 18.0f
- #define CELL_CONTENT_WIDTH 320.0f
- #define CELL_CONTENT_MARGIN 12.0f
- @interface NewsDetailViewController ()
- @property NSInteger lableCount;
- @end
- @implementation NewsDetailViewController
- @synthesize dataArray;
- @synthesize lableCount;
- @synthesize swipeGestureRecognizer;
- - (id)initWithStyle:(UITableViewStyle)style
- {
- self = [super initWithStyle:style];
- if (self) {
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIImage *myimage1=[UIImage imageNamed:@"3.jpeg"];
- UIImage *myimage2=[UIImage imageNamed:@"2.jpg"];
- 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];
- //手势识别
- self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]
- initWithTarget:self action:@selector(handleSwipes:)];
- /* Swipes that are performed from right to left are to be detected */
- self.swipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionRight;
- self.swipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionDown;
- /* Just one finger needed */
- self.swipeGestureRecognizer.numberOfTouchesRequired = 1;
- /* Add it to the view */
- [self.tableView addGestureRecognizer:self.swipeGestureRecognizer];
- self.tableView.bounces=NO;
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- self.swipeGestureRecognizer = nil;
- }
- //手势识别处理方法
- - (void) handleSwipes:(UISwipeGestureRecognizer *)paramSender{
- if (paramSender.direction & UISwipeGestureRecognizerDirectionDown){ NSLog(@"Swiped Down.");
- }
- if (paramSender.direction & UISwipeGestureRecognizerDirectionLeft){
- NSLog(@"Swiped Left."); }
- if (paramSender.direction & UISwipeGestureRecognizerDirectionRight){ NSLog(@"Swiped Right.");
- }
- if (paramSender.direction & UISwipeGestureRecognizerDirectionUp){
- NSLog(@"Swiped Up."); }
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [self.dataArray count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- static NSString *ImageCellIdentifier = @"MyTableViewImageCell";
- UILabel *label = nil;
- UITableViewCell *cell=nil;
- MyTableViewImageCell *imageCell=nil;
- //判断对象的类型
- if ([[self.dataArray objectAtIndex:[indexPath row]] isKindOfClass:[NSString class]]) { //如果是文字
- cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- CGSize size;
- if (cell == nil)
- {
- cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- label = [[UILabel alloc] initWithFrame:CGRectZero];
- [label setLineBreakMode:UILineBreakModeWordWrap];
- [label setNumberOfLines:0];
- [label setTag:1];
- [[cell contentView] addSubview:label];
- }
- NSString *text = [self.dataArray objectAtIndex:[indexPath row]];
- if (!label){ label = (UILabel*)[cell viewWithTag:1];}
- CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
- if (indexPath.row==0) { //如果是文章标题
- [label setFont:[UIFont boldSystemFontOfSize:TITLE_FONT_SIZE]];
- [label setMinimumFontSize:TITLE_FONT_SIZE];
- size = [text sizeWithFont:[UIFont boldSystemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
- }else{
- [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
- [label setMinimumFontSize:FONT_SIZE];
- size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
- }
- [label setText:text];
- [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
- }else if([[self.dataArray objectAtIndex:[indexPath row]] isKindOfClass:[UIImage class]]){ //如果是图片
- imageCell= [tableView dequeueReusableCellWithIdentifier:ImageCellIdentifier];
- if (cell==nil) {
- cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageCellIdentifier];
- }
- imageCell.myImageView.image=[self.dataArray objectAtIndex:[indexPath row]];
- imageCell.selectionStyle = UITableViewCellSelectionStyleNone;//不能被选择
- return imageCell;
- }
- cell.selectionStyle = UITableViewCellSelectionStyleNone;//不能被选择
- return cell;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- NSString *text;
- CGFloat lastHeight=0.f;
- if ([[self.dataArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]) {
- text = [self.dataArray objectAtIndex:indexPath.row];
- CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
- CGSize size;
- if (indexPath.row==0) {
- size = [text sizeWithFont:[UIFont boldSystemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
- }else{
- size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
- }
- CGFloat height = MAX(size.height, 44.0f);
- lastHeight=height + (CELL_CONTENT_MARGIN * 2);
- }else{
- if ([[self.dataArray objectAtIndex:indexPath.row] size].height>112.f) {
- lastHeight=112.f;
- }else{
- lastHeight=[[self.dataArray objectAtIndex:indexPath.row] size].height;
- }
- }
- return lastHeight;
- }
- #pragma mark - Table view delegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- }
- @end
UItableViewCell:
- // MyTableViewImageCell.h
- // SildToDo
- //
- // Created by WildCat on 13-8-18.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface MyTableViewImageCell : UITableViewCell
- @property (weak, nonatomic) IBOutlet UIImageView *myImageView;
- @end
- // MyTableViewImageCell.m
- // SildToDo
- //
- // Created by WildCat on 13-8-18.
- // Copyright (c) 2013年 wildcat. All rights reserved.
- //
- #import "MyTableViewImageCell.h"
- @implementation MyTableViewImageCell
- @synthesize myImageView;
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- // Initialization code
- }
- return self;
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated
- {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- @end
StoryBoard:
具体操作我就不说了挺简单,想知道的可以到新浪微博@我。
新浪微博:http://weibo.com/u/3202802157
转载请注明:
本文转自:http://blog.csdn.net/wildcatlele
IOS学习之路十四(用TableView做的新闻客户端展示页面)的更多相关文章
- 用TableView做的新闻客户端展示页面
用TableView做的新闻客户端展示页面 // MyTableViewImageCell.m // SildToDo // // Created by WildCat on 13-8-18. ...
- IOS学习之路十二(UITableView下拉刷新页面)
今天做了一个下拉刷新的demo,主要用到了实现的开源框架是:https://github.com/enormego/EGOTableViewPullRefresh 运行结果如下: 实现很简单下载源代码 ...
- iOS学习笔记(十四)——打电话、发短信
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- 学习之路十四:客户端调用WCF服务的几种方法小议
最近项目中接触了一点WCF的知识,也就是怎么调用WCF服务,上网查了一些资料,很快就搞出来,可是不符合头的要求,主要有以下几个方面: ①WCF的地址会变动,地址虽变,但是里面的逻辑不变! ②不要引用W ...
- 我的iOS学习之路(四):动画设置
在ios的开发过程中,经常需要对视图控件进行变化,如大小,颜色,旋转等,这是如果直接将变化结果呈现出来,就显得不够友好,所以我们通常会使用动画,让用户能够看到变化的过程. 使用动画通常有两种方式,一种 ...
- 嵌入式Linux驱动学习之路(十四)按键驱动-同步、互斥、阻塞
目的:同一个时刻,只能有一个应用程序打开我们的驱动程序. ①原子操作: v = ATOMIC_INIT( i ) 定义原子变量v并初始化为i atomic_read(v) 返回原子变量 ...
- zigbee学习之路(十四):基于协议栈的无线数据传输
一.前言 上次实验,我们介绍了zigbee原理的应用与使用,进行了基于zigbee的串口发送协议,但是上个实验并没有实现数据的收发.在这个实验中,我们要进行zigbee的接受和发送实验. 二.实验功能 ...
- IOS学习之路十五(UIView 添加背景图片以及加边框)
怎样给UIview添加背景图片呢很简单,就是先给view添加一个subview,然后设为背景图片: 效果图如下: 很简单直接上代码: //设置内容 self.myTopView.backgroundC ...
- IOS学习之路十(仿人人滑动菜单Slide-out Sidebar Menu)
最近滑动菜单比较流行,像facebook和人人等都在使用滑动菜单,今天做了一个小demo大体效果如下: 这次用了一个开源的项目ECSlidingViewController这个也是一个挺著名的托管在G ...
随机推荐
- oracle_利用闪回功能恢复数据
方便起见一般:执行如下即可不用往下看: ① 启用行移动功能 alter table tbl_a enable row movement; ② 闪回表数据到某个时间点 flashback table t ...
- JavaScript & XML
原文:JavaScript & XML 检测浏览器能力特性 //检测浏览器是否支持DOM2级XML var hasXmlDom = document.implementation.hasFea ...
- 【百度地图API】当地址解析失败时,如何调用search方法查找地址
原文:[百度地图API]当地址解析失败时,如何调用search方法查找地址 有个朋友问我,当地址解析失败时,应该如何处理呢?比如,他想搜索“南宁市青秀区”. --------------------- ...
- Asp.net MVC + EF + Spring.Net 项目实践(四)
这篇写一写如何使用Spring.net来解耦各个项目 1. 在接口层添加IStudentBLL文件,里面有GetStudent和GetAllStudents两个方法:然后在StudentBLL类里实现 ...
- 项目管理实践 -- 健身小管家(Fitness housekeeper)的管理(5)(终结)
App已经上线了,应用宝上搜索“健身小管家”即可找到,不过存在几个问题:
- inux平台的C与C++
课堂里学不到的C与C++那些事(一) 首先,声明一下这是一个系列的文章.至于整个系列有多少篇,笔者也不知道,不知道有多少篇,也不知道多久会更新一篇.反正只有一个原则,写出来的文 章能见得人才会公布出来 ...
- vim打开出现的文档^M什么
网上公开的一些代码,发现里面多^M符号.这是什么? 我搜索^M没有效果,这应该是一个特殊的控制字符.找换行的结果是不.在每一行的末尾是回车,代替它周围包裹,对于由线定义不同的编码系统是不一样的. li ...
- linux 定时关机命令
一. 关机流程 Linux 运作时, 不能够直接将电源关闭, 否则, 可能会损毁档案系统. 因此, 必须依照正常的程序关机: 观察系统使用情形(或许当时, 正有使用者做着重要的工作呢!) 通知线上使用 ...
- WeChatAPI 开源系统架构详解
WeChatAPI 开源系统架构详解 如果使用WeChatAPI,它扮演着什么样的角色? 从图中我们可以看到主要分为3个部分: 1.业务系统 2.WeChatAPI: WeChatWebAPI,主要是 ...
- 大endian和little endian
大endian和little endian 一般Intel处理器或X86平台是小端 ,只是有点老了摩托罗拉的处理器将采用大端,掌握一下小端序. 小端序一般指低地址存低字节.高地址存高 ...