zoukankan      html  css  js  c++  java
  • tableView背景

    iPhone SDK提供了默认的几个TableView样式,但是如果想提供更个性化的样式就需要自己定义。 比如添加背景

     其实自定义table view的样子很简单,无非就是把table view和table view cell的背景变成透明的,然后在指定视图和cell的背景图片(当然,也可以指定table view的背景图片)

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

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

    实现类

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

    代码中的注释已经很清楚了。 先设置视图的背景,再设定table view的背景

    再看另外一断代码,设置了cell的背景,注意,这里面使用了自定义的cell类CustomCell

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

    我们再看看如何定义自定义的cell

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

    再看实现类

    1.  
    2. #import "CustomCell.h"
    3.  
    4. @implementation CustomCell
    5.  
    6. /*—————————————————————————
    7. *
    8. *————————————————————————–*/
    9. -(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    10. {
    11.         if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    12.   {
    13.     // Cells are transparent
    14.     [self.contentView setBackgroundColor:[UIColor clearColor]];
    15.         }
    16.  
    17.         return self;
    18. }
    19.  
    20. /*—————————————————————————
    21. *
    22. *————————————————————————–*/
    23. - (void) setTheImage:(UIImage *) icon
    24. {  
    25.   // Alloc and set the frame
    26.   image = [[UIImageView alloc] initWithImage:icon];
    27.   image.frame = CGRectMake(0, 0, 286, 68);
    28.  
    29.   // Add subview
    30.   [self.contentView addSubview:image];    
    31. }
    32.  
    33. /*—————————————————————————
    34. *
    35. *————————————————————————–*/
    36. - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    37. {
    38.   [super setSelected:selected animated:animated];  
    39.   if (selected == YES)
    40.     image.alpha = .5;
    41.   else
    42.     image.alpha = 1;
    43. }
    44.  
    45. /*—————————————————————————
    46. *
    47. *————————————————————————–*/
    48. - (void)dealloc
    49. {
    50.   [image release];
    51.   [super dealloc];
    52. }
    53.  
    54. @end
    55.  
  • 相关阅读:
    任意进制间的转换
    判断线段相交 hdu 1086
    大数(高精度)加减乘除取模运算
    sqlserver2008透明书库加密
    数据库质疑
    sql2005 和sql2008 同时安装
    editrules
    sqlserver 表值函数
    sqlserver释放内存
    sql2008查看备份进度
  • 原文地址:https://www.cnblogs.com/hsxblog/p/5064957.html
Copyright © 2011-2022 走看看