成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

iPhone應用中給TableView添加背景

移動開發 iOS
自定義table view的樣子很簡單,無非就是把table view和table view cell的背景變成透明的,然后在指定視圖和cell的背景圖片(當然,也可以指定table view的背景圖片)

iPhone應用中給TableView添加背景是本文呢要介紹的內容,iPhone SDK提供了默認的幾個TableView樣式,但是如果想提供更個性化的樣式就需要自己定義。 比如添加背景

iPhone應用中給TableView添加背景

如上圖的樣子。 其實自定義table view的樣子很簡單,無非就是把table view和table view cell的背景變成透明的,然后在指定視圖和cell的背景圖片(當然,也可以指定table view的背景圖片)

  1. @interface MainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>    
  2.  {    
  3.   UITableView *theTableView;    
  4.  } 

先建立Controller,注意是繼承自UIViewController而不是UITableViewController

  1. - (id)init    
  2. {    
  3.    if (self = [super init])     
  4.   {    
  5.     self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];         
  6.      // Setup the background    
  7.     UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];    
  8.      [self.view addSubview:background];    
  9.     [background release];    
  10.       
  11.      // Create table view    
  12.      theTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 11, 320, 460) style: UITableViewStylePlain];    
  13.      [theTableView setDelegate:self];    
  14.     [theTableView setDataSource:self];    
  15.        
  16.     // This should be set to work with the image height    
  17.      [theTableView setRowHeight:68];    
  18.      // Transparent, so we can see the background    
  19.     [theTableView setBackgroundColor:[UIColor clearColor]];    
  20.    [theTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];    
  21.     [theTableView setIndicatorStyle:UIScrollViewIndicatorStyleWhite];    
  22.        
  23.     [self.view addSubview:theTableView];    
  24.   }    
  25.    return self;    
  26.  }  

代碼中的注釋已經很清楚了。 先設置視圖的背景,再設定table view的背景

再看另外一斷代碼,設置了cell的背景,注意,這里面使用了自定義的cell類CustomCell

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath     
  2.  
  3. {    
  4.          CustomCell *cell= [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];     
  5.          // Default to no selected style and not selected    
  6.          cell.selectionStyle = UITableViewCellSelectionStyleNone;    
  7.          // Set the image for the cell    
  8.         [cell setTheImage:[UIImage imageNamed:[NSString stringWithFormat:@"Arrows%d.png", indexPath.row + 1]]];    
  9.              
  10.         return cell;    
  11.  }  

我們再看看如何定義自定義的cell

  1. #import <UIKit/UIKit.h>    
  2.  @interface CustomCell : UITableViewCell     
  3.  {    
  4.    UIImageView *image;     
  5.  }    
  6.     
  7. - (void) setTheImage:(UIImage *)icon;    
  8.  @end  

再看實現類

  1. #import "CustomCell.h"    
  2.  @implementation CustomCell    
  3.  /*—————————————————————————    
  4.  *     
  5.  *————————————————————————–*/   
  6.  -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier    
  7. {    
  8.          if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])     
  9.    {    
  10.      // Cells are transparent    
  11.     [self.contentView setBackgroundColor:[UIColor clearColor]];    
  12.         }    
  13.          return self;    
  14.  }    
  15.  /*—————————————————————————    
  16.  *     
  17.  *————————————————————————–*/   
  18.  - (void) setTheImage:(UIImage *) icon    
  19. {      
  20.    // Alloc and set the frame    
  21.    image = [[UIImageView alloc] initWithImage:icon];    
  22.    image.frame = CGRectMake(0, 0, 286, 68);    
  23.        
  24.    // Add subview    
  25.    [self.contentView addSubview:image];        
  26. }    
  27.  /*—————————————————————————    
  28.  *    
  29.  *————————————————————————–*/   
  30.  - (void)setSelected:(BOOL)selected animated:(BOOL)animated     
  31.  {    
  32.    [super setSelected:selected animated:animated];       
  33.    if (selected == YES)    
  34.      image.alpha = .5;    
  35.   else   
  36.      image.alpha = 1;    
  37.  }    
  38.  /*—————————————————————————    
  39.  *     
  40.  *————————————————————————–*/   
  41.  - (void)dealloc     
  42. {    
  43.    [image release];    
  44.    [super dealloc];    
  45.  }    
  46.  @end  

還是很簡單的吧。

小結:iPhone應用中給TableView添加背景的內容介紹完了,希望通過本文的學習對你能有所幫助!

責任編輯:zhaolei 來源: 互聯網
相關推薦

2011-08-12 14:58:43

iPhoneTableview數據

2011-09-13 11:41:18

2011-08-19 10:35:19

iPhone應用Three20

2015-07-16 09:59:29

壁紙linux

2011-08-02 17:27:06

iPhone應用 剪切技巧

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-02 17:14:41

iPhone應用 UITableVie

2011-08-22 14:12:48

iPhone開發NSTableView

2011-09-15 15:58:37

iPhone應用Quick Snap拍攝工具

2011-08-15 11:37:20

iPhone開發Mask

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-12 14:33:06

iPhone緩存文件

2011-08-15 15:44:46

iPhone開發PDF

2011-08-18 16:24:44

iPhone開發圖片

2011-09-07 16:24:10

Qt Widget

2011-08-10 17:30:50

iphoneThree20

2011-08-18 17:24:34

iPhone開發UINavigatio

2011-07-21 15:49:27

iPhone 模擬器 視頻

2011-08-16 18:56:11

iPhone開發Three20

2010-11-05 13:02:58

內存iPhone
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 正在播放国产精品 | 国产高清免费 | 国产欧美久久一区二区三区 | 一级欧美一级日韩片免费观看 | 中国美女撒尿txxxxx视频 | 久久精品97 | 亚洲精品日韩精品 | 亚洲欧美日韩在线一区二区 | 精品不卡| 久久久资源 | 国产在线精品一区 | 亚洲欧美精品国产一级在线 | 国产视频中文字幕 | 成人三级视频 | 日韩视频―中文字幕 | 欧美一级片a | 国内av在线| 国产精品不卡 | av中文在线播放 | 亚洲综合99 | 在线伊人 | 欧美久久一级 | 亚洲一区二区三区四区五区午夜 | 欧美激情久久久 | 国产日韩欧美在线 | 成人一级片在线观看 | 中文字幕日韩欧美一区二区三区 | 日韩一区二区三区在线播放 | 国偷自产av一区二区三区 | 国产精品二区三区在线观看 | 黄色大片免费网站 | 国产一区二区三区在线免费观看 | 国外成人在线视频网站 | 99免费在线视频 | 天天看逼| 日本三级线观看 视频 | 黑色丝袜三级在线播放 | 国产国语精品 | 日韩高清一区二区 | 亚洲一区国产 | 91素人 |