iOS 中快速簡單高效的實現自定義tableViewCell 的方法-親測實戰版本-精華版

 

IOS7學習之路一(新UI之自定義UITableViewCell)

分類: IOS 5167人閱讀 評論(5) 收藏 舉報

ios7 新升級之後界面有了很大的變化,xcode模擬器去掉了手機邊框和home鍵,如果想回到主頁面,可以按住shift+comment+r鍵。廢話少說先展示一下新UI下UItableView設置爲Group後的效果:



整體界面顯得更加簡潔,而且UITableViewCell的寬度默認爲滿屛,也取消了圓角。

下面說下自定義UITableView的過程:

首先在storyboard中給cell拖過來一個UIimageView和兩個label 



然後新建一個MyCell類繼承自UITableViewCell。

MyCell代碼:

  1. //  MyCell.h  
  2. //  XcodeTest  
  3. //  
  4. //  Created by wildcat on 13-11-7.  
  5. //  Copyright (c) 2013年 wildcat. All rights reserved.  
  6. //  
  7.   
  8. #import <UIKit/UIKit.h>  
  9.   
  10. @interface MyCell : UITableViewCell  
  11. @property (weak, nonatomic) IBOutlet UIImageView *myImageView;  
  12. @property (weak, nonatomic) IBOutlet UILabel *nameLabel;  
  13. @property (weak, nonatomic) IBOutlet UILabel *timeLabel;  
  14.   
  15. @end  

  1. //  MyCell.m  
  2. //  XcodeTest  
  3. //  
  4. //  Created by wildcat on 13-11-7.  
  5. //  Copyright (c) 2013年 wildcat. All rights reserved.  
  6. //  
  7.   
  8. #import "MyCell.h"  
  9.   
  10. @implementation MyCell  
  11.   
  12. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  
  13. {  
  14.     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];  
  15.     if (self) {  
  16.         // Initialization code  
  17.     }  
  18.     return self;  
  19. }  
  20.   
  21. - (void)setSelected:(BOOL)selected animated:(BOOL)animated  
  22. {  
  23.     [super setSelected:selected animated:animated];  
  24.   
  25.     // Configure the view for the selected state  
  26. }  
  27. #pragma mark 設置Cell的邊框寬度  
  28. - (void)setFrame:(CGRect)frame {  
  29.     frame.origin.x += 10;  
  30.     frame.size.width -= 22 * 10;  
  31.     [super setFrame:frame];  
  32. }  
  33.   
  34. @end  


使用:

在UITableViewController中使用,代碼如下:


  1. //  
  2. //  RootViewController.m  
  3. //  XcodeTest  
  4. //  
  5. //  Created by wildcat on 13-11-7.  
  6. //  Copyright (c) 2013年 wildcat. All rights reserved.  
  7. //  
  8.   
  9. #import "RootViewController.h"  
  10. #import "MyCell.h"  
  11. @interface RootViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation RootViewController  
  16.   
  17. - (id)initWithStyle:(UITableViewStyle)style  
  18. {  
  19.     self = [super initWithStyle:style];  
  20.     if (self) {  
  21.         // Custom initialization  
  22.     }  
  23.     return self;  
  24. }  
  25.   
  26. - (void)viewDidLoad  
  27. {  
  28.     [super viewDidLoad];  
  29.       
  30.   
  31. }  
  32.   
  33. - (void)didReceiveMemoryWarning  
  34. {  
  35.     [super didReceiveMemoryWarning];  
  36.     // Dispose of any resources that can be recreated.  
  37. }  
  38.   
  39. #pragma mark - Table view data source  
  40.   
  41. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  42. {  
  43.     return 2;  
  44. }  
  45.   
  46. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  47. {  
  48.     return 3;  
  49. }  
  50.   
  51. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  52. {  
  53.     static NSString *CellIdentifier = @"Cell";  
  54.     MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];  
  55.       
  56.     if (cell==nil) {  
  57.         cell=[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
  58.     }  
  59.       
  60.     cell.nameLabel.text=@"WildCat";  
  61.     cell.timeLabel.text=@"2013-11-7";  
  62.     return cell;  
  63. }  
  64.   
  65. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  66.   
  67.     return  75.f;  
  68. }  
  69.   
  70.   
  71. @end  

修改後的效果:


當然如果你喜歡cell滿屏的效果,你完全可以不設cell的寬度。。。

轉載請註明:版權所有http://1.wildcat.sinaapp.com/


發佈了35 篇原創文章 · 獲贊 2 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章