自定義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 }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM