最近作的也個項目,要作一個IOS的新聞展現view(有圖有文字,不用UIwebview,由於數據是用webservice解析的到的json數據),本身一直沒有頭緒,可後來聽一個學長說能夠用listview.。但我查了查ios好像沒有listview。因而就用UITableView和自定義cell解決了這個問題。html
效果圖以下:ios
UITableView:web
- //
- // 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:json
- // 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:ui
具體操做我就不說了挺簡單,想知道的能夠到新浪微博@我。atom
新浪微博:http://weibo.com/u/3202802157
轉載請註明:本文轉自:http://blog.csdn.net/wildcatlele