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-08-19 10:35:19

iPhone应用Three20

2011-09-13 11:41:18

2015-07-16 09:59:29

壁纸linux

2011-09-07 16:24:10

Qt Widget

2011-08-02 17:14:41

iPhone应用 UITableVie

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-02 17:27:06

iPhone应用 剪切技巧

2011-08-10 17:30:50

iphoneThree20

2011-08-18 17:24:34

iPhone开发UINavigatio

2011-07-21 15:49:27

iPhone 模拟器 视频

2011-08-15 15:44:46

iPhone开发PDF

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-12 14:33:06

iPhone缓存文件

2011-08-18 16:24:44

iPhone开发图片

2011-08-16 18:56:11

iPhone开发Three20

2011-09-15 15:58:37

iPhone应用Quick Snap拍摄工具

2011-08-22 14:12:48

iPhone开发NSTableView

2011-08-15 11:37:20

iPhone开发Mask

2011-08-02 18:30:32

iOS 应用程序 属性
点赞
收藏

51CTO技术栈公众号