iPhone应用中给TableView添加背景


  本文标签:iPhone TableView 背景

  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添加背景的内容介绍完了,希望通过本文的学习对你能有所帮助!