自定义UITableViewCell

1 @interface CustomCell : UITableViewCell
2 
3 @property (nonatomic, retain) IBOutlet UIImageView *tripPhoto;
4 @property (nonatomic, retain) IBOutlet UILabel *tripName;
5 
6 @end

 

1 #import "CustomCell.h"
2 
3 @implementation CustomCell
4 
5 @synthesize tripPhoto, tripName;
6 
7 @end

同时定义4种类型的自定义单元格的.xib文件,分别是:

TopRow.xib     (最上面一行)

MiddleRow.xib   (中间某行)

BottomRow.xib  (最下面一行)

SingleRow.xib   (单独一行)

 

  1 #import "SurfsUpViewController.h"
  2 #import "CustomCell.h"
  3 #import "PlaceholderViewController.h"
  4 
  5 NSString * const REUSE_ID_TOP = @"TopRow";
  6 NSString * const REUSE_ID_MIDDLE = @"MiddleRow";
  7 NSString * const REUSE_ID_BOTTOM = @"BottomRow";
  8 NSString * const REUSE_ID_SINGLE = @"SingleRow";
  9 
 10 @implementation SurfsUpViewController
 11 
 12 
 13 #pragma mark 根据行标记取标题
 14 - (NSString *)tripNameForRowAtIndexPath:(NSIndexPath *)indexPath{
 15     switch (indexPath.row){
 16         case 0:
 17             return @"Kuta, Bali";
 18             break;
 19         case 1:
 20             return @"Lagos, Portugal";
 21             break;
 22         case 2:
 23             return @"Waikiki, Hawaii";
 24             break;
 25     }
 26     return @"-";
 27 }
 28 
 29 #pragma mark 根据行标记取图片
 30 - (UIImage *)tripPhotoForRowAtIndexPath:(NSIndexPath *)indexPath{
 31     switch (indexPath.row){
 32         case 0:
 33             return [UIImage imageNamed:@"surf1.png"];
 34             break;
 35         case 1:
 36             return [UIImage imageNamed:@"surf2.png"];
 37             break;
 38         case 2:
 39             return [UIImage imageNamed:@"surf3.png"];
 40             break;
 41     }
 42     return nil;
 43 }
 44 
 45 #pragma mark 根据行标记取唯一标示
 46 - (NSString *)reuseIdentifierForRowAtIndexPath:(NSIndexPath *)indexPath{
 47     NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
 48     NSInteger rowIndex = indexPath.row;
 49     
 50     if (rowCount == 1){
 51         return REUSE_ID_SINGLE;     // 单独一行
 52     }
 53     
 54     if (rowIndex == 0){
 55         return REUSE_ID_TOP;       // 第一行
 56     }
 57     
 58     if (rowIndex == (rowCount - 1)){    // 最后一行
 59         return REUSE_ID_BOTTOM;
 60     }
 61     return REUSE_ID_MIDDLE;         // 中间某行
 62 }
 63 
 64 #pragma mark 根据行标记返回“默认”状态下的单元格背景图
 65 - (UIImage *)backgroundImageForRowAtIndexPath:(NSIndexPath *)indexPath
 66 {
 67     NSString *reuseID = [self reuseIdentifierForRowAtIndexPath:indexPath];
 68     if ([REUSE_ID_SINGLE isEqualToString:reuseID] == YES)
 69     {
 70         UIImage *background = [UIImage imageNamed:@"table_cell_single.png"]; 
 71         return [background resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 43.0, 0.0, 64.0)];
 72     }
 73     else if ([REUSE_ID_TOP isEqualToString:reuseID] == YES)
 74     {
 75         UIImage *background = [UIImage imageNamed:@"table_cell_top.png"]; 
 76         return [background resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 43.0, 0.0, 64.0)];
 77     }
 78     else if ([REUSE_ID_BOTTOM isEqualToString:reuseID] == YES)
 79     {
 80         UIImage *background = [UIImage imageNamed:@"table_cell_bottom.png"]; 
 81         return [background resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 34.0, 0.0, 35.0)];
 82     }
 83     else    // REUSE_ID_MIDDLE
 84     {
 85         UIImage *background = [UIImage imageNamed:@"table_cell_mid.png"]; 
 86         return [background resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 30.0, 0.0, 30.0)];
 87     }
 88 }
 89 
 90 #pragma mark 根据行标记返回“选中”状态下的单元格背景图
 91 - (UIImage *)selectedBackgroundImageForRowAtIndexPath:(NSIndexPath *)indexPath
 92 {
 93     NSString *reuseID = [self reuseIdentifierForRowAtIndexPath:indexPath];
 94     if ([REUSE_ID_SINGLE isEqualToString:reuseID] == YES){
 95         UIImage *background = [UIImage imageNamed:@"table_cell_single_sel.png"]; 
 96         return [background resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 43.0, 0.0, 64.0)];
 97     }
 98     else if ([REUSE_ID_TOP isEqualToString:reuseID] == YES){
 99         UIImage *background = [UIImage imageNamed:@"table_cell_top_sel.png"]; 
100         return [background resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 43.0, 0.0, 64.0)];
101     }
102     else if ([REUSE_ID_BOTTOM isEqualToString:reuseID] == YES){
103         UIImage *background = [UIImage imageNamed:@"table_cell_bottom_sel.png"]; 
104         return [background resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 34.0, 0.0, 35.0)];
105     }
106     else{    // REUSE_ID_MIDDLE
107         UIImage *background = [UIImage imageNamed:@"table_cell_mid_sel.png"]; 
108         return [background resizableImageWithCapInsets:UIEdgeInsetsMake(0.0, 30.0, 0.0, 30.0)];
109     }
110 }
111 
112 #pragma mark 注册不同的Nib单元格
113 - (void)registerNIBs{
114     NSBundle *classBundle = [NSBundle bundleForClass:[CustomCell class]];           //  得到CustomCell类的Bundle文件
115     
116     UINib *topNib = [UINib nibWithNibName:REUSE_ID_TOP bundle:classBundle];         //  根据特定Bundle和Nib文件名创建UINib文件
117     [[self tableView] registerNib:topNib forCellReuseIdentifier:REUSE_ID_TOP];      //  将此UINib文件注册给特定单元格
118     
119     UINib *middleNib = [UINib nibWithNibName:REUSE_ID_MIDDLE bundle:classBundle];    
120     [[self tableView] registerNib:middleNib forCellReuseIdentifier:REUSE_ID_MIDDLE];
121     
122     UINib *bottomNib = [UINib nibWithNibName:REUSE_ID_BOTTOM bundle:classBundle];    
123     [[self tableView] registerNib:bottomNib forCellReuseIdentifier:REUSE_ID_BOTTOM];
124     
125     UINib *singleNib = [UINib nibWithNibName:REUSE_ID_SINGLE bundle:classBundle];    
126     [[self tableView] registerNib:singleNib forCellReuseIdentifier:REUSE_ID_SINGLE];    
127 }
128 
129 #pragma mark - View lifecycle
130 
131 - (void)viewDidLoad {
132     [super viewDidLoad];
133     
134     [self registerNIBs];  // 注册4种不用的单元格
135     
136     [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];  // 设置单元格之间的分割线样式
137     [self.tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_sand.png"]]]; // 设置表格背景
138 }
139 
140 #pragma mark - UITableViewCell
141 
142 - (void)configureCell:(CustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
143     [[cell tripPhoto] setImage:[self tripPhotoForRowAtIndexPath:indexPath]]; // 根据行标志设置单元格的图片
144     [[cell tripName] setText:[self tripNameForRowAtIndexPath:indexPath]];    // 根据行标示设置单元格的标题
145     
146     CGRect cellRect = [cell frame];     // 得到单元格尺寸
147     UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:cellRect];     // 定义背景图View
148     [backgroundView setImage:[self backgroundImageForRowAtIndexPath:indexPath]];    // 根据行标记得到背景图片,然后设置单元格背景View
149     [cell setBackgroundView:backgroundView];        // 将UIImageView赋值给单元格“默认”时的背景
150     
151     UIImageView *selectedBackgroundView = [[UIImageView alloc] initWithFrame:cellRect];
152     [selectedBackgroundView setImage:[self selectedBackgroundImageForRowAtIndexPath:indexPath]];     
153     [cell setSelectedBackgroundView:selectedBackgroundView];  // 同理设置单元格“选中”时的背景
154 }
155 
156 #pragma mark - UITableViewDataSource
157 
158 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
159 {
160     NSString *reuseID = [self reuseIdentifierForRowAtIndexPath:indexPath];      // 根据行标记返回特定标示
161     UITableViewCell *cell = [[self tableView] dequeueReusableCellWithIdentifier:reuseID];  // 根据特定标示得到单元格
162     [self configureCell:(CustomCell *)cell forRowAtIndexPath:indexPath];        
163     return cell;
164 }
165 
166 #pragma mark - UITableViewDelegate
167 
168 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
169 {
170     UITableViewCell *cell = [self tableView:[self tableView] cellForRowAtIndexPath:indexPath];
171     return [cell frame].size.height;
172 }
173 
174 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
175     return 1;
176 }
177 
178 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
179     return 3;
180 }

 

 

posted @ 2013-02-20 08:58  diablo大王  阅读(3084)  评论(0编辑  收藏  举报