自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iPhone應(yīng)用中給TableView添加背景

移動(dòng)開(kāi)發(fā) iOS
自定義table view的樣子很簡(jiǎn)單,無(wú)非就是把table view和table view cell的背景變成透明的,然后在指定視圖和cell的背景圖片(當(dāng)然,也可以指定table view的背景圖片)

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

iPhone應(yīng)用中給TableView添加背景

如上圖的樣子。 其實(shí)自定義table view的樣子很簡(jiǎn)單,無(wú)非就是把table view和table view cell的背景變成透明的,然后在指定視圖和cell的背景圖片(當(dāng)然,也可以指定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.  }  

代碼中的注釋已經(jīng)很清楚了。 先設(shè)置視圖的背景,再設(shè)定table view的背景

再看另外一斷代碼,設(shè)置了cell的背景,注意,這里面使用了自定義的cell類(lèi)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.  }  

我們?cè)倏纯慈绾味x自定義的cell

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

再看實(shí)現(xiàn)類(lèi)

  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  

還是很簡(jiǎn)單的吧。

小結(jié):iPhone應(yīng)用中給TableView添加背景的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)對(duì)你能有所幫助!

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-12 14:58:43

iPhoneTableview數(shù)據(jù)

2011-08-19 10:35:19

iPhone應(yīng)用Three20

2011-09-13 11:41:18

2015-07-16 09:59:29

壁紙linux

2011-08-02 17:27:06

iPhone應(yīng)用 剪切技巧

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-02 17:14:41

iPhone應(yīng)用 UITableVie

2011-09-07 16:24:10

Qt Widget

2011-08-10 17:30:50

iphoneThree20

2011-08-18 17:24:34

iPhone開(kāi)發(fā)UINavigatio

2011-08-22 14:12:48

iPhone開(kāi)發(fā)NSTableView

2011-08-15 11:37:20

iPhone開(kāi)發(fā)Mask

2011-09-15 15:58:37

iPhone應(yīng)用Quick Snap拍攝工具

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-12 14:33:06

iPhone緩存文件

2011-08-15 15:44:46

iPhone開(kāi)發(fā)PDF

2011-08-18 16:24:44

iPhone開(kāi)發(fā)圖片

2011-07-21 15:49:27

iPhone 模擬器 視頻

2011-08-16 18:56:11

iPhone開(kāi)發(fā)Three20

2010-11-05 13:02:58

內(nèi)存iPhone
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)