代码改变世界

IOS学习之路十四(用TableView做的新闻客户端展示页面)

2013-09-09 22:01  Lves Li  阅读(1240)  评论(0编辑  收藏  举报

最近做的也个项目,要做一个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.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface NewsDetailViewController : UITableViewController  
  12. @property (nonatomic,copy) NSArray *dataArray;  
  13. @property (nonatomic, strong)  
  14. UISwipeGestureRecognizer *swipeGestureRecognizer;  
  15.   
  16. @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.   
  9. #import "NewsDetailViewController.h"  
  10. #import "MyTableViewImageCell.h"  
  11.   
  12. #define FONT_SIZE 14.0f  
  13. #define TITLE_FONT_SIZE 18.0f  
  14. #define CELL_CONTENT_WIDTH 320.0f  
  15. #define CELL_CONTENT_MARGIN 12.0f  
  16. @interface NewsDetailViewController ()  
  17. @property NSInteger  lableCount;  
  18. @end  
  19.   
  20. @implementation NewsDetailViewController  
  21. @synthesize dataArray;  
  22. @synthesize lableCount;  
  23. @synthesize swipeGestureRecognizer;  
  24. - (id)initWithStyle:(UITableViewStyle)style  
  25. {  
  26.     self = [super initWithStyle:style];  
  27.     if (self) {  
  28.           
  29.     }  
  30.     return self;  
  31. }  
  32.   
  33. - (void)viewDidLoad  
  34. {  
  35.     [super viewDidLoad];  
  36.      
  37.     UIImage *myimage1=[UIImage imageNamed:@"3.jpeg"];  
  38.     UIImage *myimage2=[UIImage imageNamed:@"2.jpg"];  
  39.          
  40.     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];  
  41.       
  42.     //手势识别  
  43.     self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]  
  44.                                    initWithTarget:self action:@selector(handleSwipes:)];  
  45.     /* Swipes that are performed from right to left are to be detected */  
  46.     self.swipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionRight;  
  47.     self.swipeGestureRecognizer.direction=UISwipeGestureRecognizerDirectionDown;  
  48.       
  49.     /* Just one finger needed */  
  50.     self.swipeGestureRecognizer.numberOfTouchesRequired = 1;  
  51.     /* Add it to the view */  
  52.     [self.tableView addGestureRecognizer:self.swipeGestureRecognizer];  
  53.       
  54.     self.tableView.bounces=NO;  
  55. }  
  56.   
  57. - (void)viewDidUnload  
  58. {  
  59.     [super viewDidUnload];  
  60.   self.swipeGestureRecognizer = nil;  
  61. }  
  62. //手势识别处理方法  
  63. - (void) handleSwipes:(UISwipeGestureRecognizer *)paramSender{  
  64.     if (paramSender.direction & UISwipeGestureRecognizerDirectionDown){ NSLog(@"Swiped Down.");  
  65.     }  
  66.     if (paramSender.direction & UISwipeGestureRecognizerDirectionLeft){  
  67.         NSLog(@"Swiped Left."); }  
  68.     if (paramSender.direction & UISwipeGestureRecognizerDirectionRight){ NSLog(@"Swiped Right.");  
  69.     }  
  70.     if (paramSender.direction & UISwipeGestureRecognizerDirectionUp){  
  71.         NSLog(@"Swiped Up."); }  
  72. }  
  73.   
  74.   
  75.   
  76.   
  77. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  78. {  
  79.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  80. }  
  81.   
  82. #pragma mark - Table view data source  
  83.   
  84. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  85. {  
  86.     return 1;  
  87. }  
  88.   
  89. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  90. {  
  91.     return [self.dataArray count];  
  92. }  
  93.   
  94. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  95. {  
  96.     static NSString *CellIdentifier = @"Cell";  
  97.     static NSString *ImageCellIdentifier = @"MyTableViewImageCell";  
  98.       
  99.     UILabel *label = nil;  
  100.     UITableViewCell *cell=nil;      
  101.     MyTableViewImageCell *imageCell=nil;  
  102.     //判断对象的类型  
  103.     if ([[self.dataArray objectAtIndex:[indexPath row]] isKindOfClass:[NSString class]]) { //如果是文字  
  104.         cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  105.         CGSize size;  
  106.         if (cell == nil)  
  107.         {  
  108.             cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
  109.             label = [[UILabel alloc] initWithFrame:CGRectZero];  
  110.             [label setLineBreakMode:UILineBreakModeWordWrap];  
  111.             [label setNumberOfLines:0];  
  112.             [label setTag:1];  
  113.             [[cell contentView] addSubview:label];  
  114.                          
  115.         }  
  116.         NSString *text = [self.dataArray objectAtIndex:[indexPath row]];  
  117.           
  118.                  
  119.         if (!label){ label = (UILabel*)[cell viewWithTag:1];}  
  120.         CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);  
  121.          
  122.         if (indexPath.row==0) {  //如果是文章标题  
  123.             [label setFont:[UIFont  boldSystemFontOfSize:TITLE_FONT_SIZE]];  
  124.             [label setMinimumFontSize:TITLE_FONT_SIZE];  
  125.             size = [text sizeWithFont:[UIFont  boldSystemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];  
  126.               
  127.         }else{  
  128.             [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];  
  129.             [label setMinimumFontSize:FONT_SIZE];  
  130.             size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];  
  131.         }  
  132.           
  133.         [label setText:text];  
  134.         [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];  
  135.     }else if([[self.dataArray objectAtIndex:[indexPath row]] isKindOfClass:[UIImage class]]){ //如果是图片  
  136.        imageCell= [tableView dequeueReusableCellWithIdentifier:ImageCellIdentifier];  
  137.         if (cell==nil) {  
  138.             cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageCellIdentifier];  
  139.         }  
  140.         imageCell.myImageView.image=[self.dataArray objectAtIndex:[indexPath row]];  
  141.         imageCell.selectionStyle = UITableViewCellSelectionStyleNone;//不能被选择  
  142.         return imageCell;  
  143.     }  
  144.     cell.selectionStyle = UITableViewCellSelectionStyleNone;//不能被选择  
  145.     return  cell;  
  146. }  
  147.   
  148. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  149.       
  150.     NSString *text;  
  151.     CGFloat lastHeight=0.f;  
  152.     if ([[self.dataArray objectAtIndex:indexPath.row] isKindOfClass:[NSString class]]) {  
  153.         text = [self.dataArray objectAtIndex:indexPath.row];  
  154.         CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);  
  155.         CGSize size;  
  156.         if (indexPath.row==0) {  
  157.             size = [text sizeWithFont:[UIFont boldSystemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];  
  158.         }else{  
  159.             size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];  
  160.         }  
  161.         CGFloat height = MAX(size.height, 44.0f);  
  162.         lastHeight=height + (CELL_CONTENT_MARGIN * 2);  
  163.           
  164.     }else{  
  165.        
  166.         if ([[self.dataArray objectAtIndex:indexPath.row] size].height>112.f) {  
  167.             lastHeight=112.f;  
  168.         }else{  
  169.             lastHeight=[[self.dataArray objectAtIndex:indexPath.row] size].height;  
  170.         }  
  171.     }  
  172.     return lastHeight;  
  173.       
  174. }  
  175.   
  176. #pragma mark - Table view delegate  
  177.   
  178. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  179. {  
  180. }  
  181.   
  182. @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.   
  8. #import <UIKit/UIKit.h>  
  9.   
  10. @interface MyTableViewImageCell : UITableViewCell  
  11.   
  12. @property (weak, nonatomic) IBOutlet UIImageView *myImageView;  
  13. @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.   
  8. #import "MyTableViewImageCell.h"  
  9.   
  10. @implementation MyTableViewImageCell  
  11. @synthesize myImageView;  
  12. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  13. {  
  14.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  15.     if (self) {  
  16.         // Initialization code  
  17.     }  
  18.     return self;  
  19. }  
  20.   
  21. - (void)setSelected:(BOOL)selected animated:(BOOL)animated  
  22. {  
  23.     [super setSelected:selected animated:animated];  
  24.   
  25.     // Configure the view for the selected state  
  26. }  
  27.   
  28. @end  



 

StoryBoard:

 

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

 

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

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