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

时间:2024-04-02 15:37:02

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

效果图如下:

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

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:

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

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

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

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