iOS開發UI篇—UITableview控件基本使用 – iPhone手機開發技術文章 iPhone軟體開發教學課程

iOS開發UI篇—UITableview控件基本使用

 

一、一個簡單的英雄展示程序

 

NJHero.h文件代碼(字典轉模型)

 

復制代碼

 1 #import <Foundation/Foundation.h>

 2 

 3 @interface NJHero : NSObject

 4 /**

 5  *  頭像

 6  */

 7 @property (nonatomic, copy) NSString *icon;

 8 /**

 9  *  名稱

10  */

11 @property (nonatomic, copy) NSString *name;

12 /**

13  *  描述

14  */

15 @property (nonatomic, copy) NSString *intro;

16 

17 – (instancetype)initWithDict:(NSDictionary *)dict;

18 + (instancetype)heroWithDict:(NSDictionary *)dict;

19 @end

復制代碼

NJViewController.m文件代碼

 

復制代碼

 1 #import "NJViewController.h"

 2 #import "NJHero.h"

 3 

 4 @interface NJViewController ()<UITableViewDataSource, UITableViewDelegate>

 5 /**

 6  *  保存所有的英雄數據

 7  */

 8 @property (nonatomic, strong) NSArray *heros;

 9 @property (weak, nonatomic) IBOutlet UITableView *tableView;

10 

11 @end

12 

13 @implementation NJViewController

14 

15 #pragma mark – 懶加載

16 – (NSArray *)heros

17 {

18     if (_heros == nil) {

19         // 1.獲得全路徑

20         NSString *fullPath =  [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];

21         // 2.更具全路徑加載數據

22         NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];

23         // 3.字典轉模型

24         NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];

25         for (NSDictionary *dict in dictArray) {

26             NJHero *hero = [NJHero heroWithDict:dict];

27             [models addObject:hero];

28         }

29         // 4.賦值數據

30         _heros = [models copy];

31     }

32     // 4.返回數據

33     return _heros;

34 }

35 

36 – (void)viewDidLoad

37 {

38     [super viewDidLoad];

39     // 設置Cell的高度

40     // 當每一行的cell高度一致的時候使用屬性設置cell的高度

41     self.tableView.rowHeight = 60;

42     self.tableView.delegate = self;

43 }

44 

45 #pragma mark – UITableViewDataSource

46 // 返回多少組

47 – (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

48 {

49     return 1;

50 }

51 // 返回每一組有多少行

52 – (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

53 {

54     return self.heros.count;

55 }

56 // 返回哪一組的哪一行顯示什麼內容

57 – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

58 {

59     // 1.創建CELL

60     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

61     // 2.設置數據

62     // 2.1取出對應行的模型

63     NJHero *hero = self.heros[indexPath.row];

64     // 2.2賦值對應的數據

65     cell.textLabel.text = hero.name;

66     cell.detailTextLabel.text = hero.intro;

67     cell.imageView.image = [UIImage imageNamed:hero.icon];

68     // 3.返回cell

69     return cell;

70 }

71 #pragma mark – UITableViewDelegate

72 /*

73 // 當每一行的cell的高度不一致的時候就使用代理方法設置cell的高度

74 – (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

75 {

76     if (1 == indexPath.row) {

77         return 180;

78     }

79     return 44;

80 }

81  */

82 

83 #pragma mark – 控制狀態欄是否顯示

84 /**

85  *   返回YES代表隱藏狀態欄, NO相反

86  */

87 – (BOOL)prefersStatusBarHidden

88 {

89     return YES;

90 }

91 @end

復制代碼

實現效果:

 

 

 

代碼註意點:

 

(1)在字典轉模型的代碼處用下面的代碼,為可變數組分配dictArray.count個存儲空間,可以提高程序的性能

 

NSMutableArray *models = [NSMutableArrayarrayWithCapacity:dictArray.count];

 

(2)設置cell的高度

 

有三種辦法可以設置cell的高度

 

1) 可以在初始加載方法中設置,self.tableView.rowHeight = 60;這適用於當每一行的cell高度一致的時候,使用屬性設置cell的高度。

 

2)在storyboard中設置,適用於高度一致

 

3)當每一行的cell的高度不一致的時候就使用代理方法設置cell的高度

 

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

 

{

 

    if (1 == indexPath.row) {

 

        return 180;

 

    }

 

    return 44;

 

}

 

二、cell的一些屬性

 

代碼示例:

 

復制代碼

  1 #import "NJViewController.h"

  2 #import "NJHero.h"

  3 

  4 @interface NJViewController ()<UITableViewDataSource, UITableViewDelegate>

  5 /**

  6  *  保存所有的英雄數據

  7  */

  8 @property (nonatomic, strong) NSArray *heros;

  9 @property (weak, nonatomic) IBOutlet UITableView *tableView;

 10 

 11 @end

 12 

 13 @implementation NJViewController

 14 

 15 #pragma mark – 懶加載

 16 – (NSArray *)heros

 17 {

 18     if (_heros == nil) {

 19         // 1.獲得全路徑

 20         NSString *fullPath =  [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];

 21         // 2.更具全路徑加載數據

 22         NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];

 23         // 3.字典轉模型

 24         NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];

 25         for (NSDictionary *dict in dictArray) {

 26             NJHero *hero = [NJHero heroWithDict:dict];

 27             [models addObject:hero];

 28         }

 29         // 4.賦值數據

 30         _heros = [models copy];

 31     }

 32     // 4.返回數據

 33     return _heros;

 34 }

 35 

 36 – (void)viewDidLoad

 37 {

 38     [super viewDidLoad];

 39     // 設置Cell的高度

 40     // 當每一行的cell高度一致的時候使用屬性設置cell的高度

 41     self.tableView.rowHeight = 60;

 42     self.tableView.delegate = self;

 43 

 44 }

 45 

 46 #pragma mark – UITableViewDataSource

 47 // 返回多少組

 48 – (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

 49 {

 50     return 1;

 51 }

 52 // 返回每一組有多少行

 53 – (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

 54 {

 55     return self.heros.count;

 56 }

 57 // 返回哪一組的哪一行顯示什麼內容

 58 – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 59 {

 60     // 1.創建CELL

 61     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

 62     // 2.設置數據

 63     // 2.1取出對應行的模型

 64     NJHero *hero = self.heros[indexPath.row];

 65     // 2.2賦值對應的數據

 66     cell.textLabel.text = hero.name;

 67     cell.detailTextLabel.text = hero.intro;

 68     cell.imageView.image = [UIImage imageNamed:hero.icon];

 69     

 70     // 2.3設置cell的輔助視圖

 71     // cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;

 72     if (0 == indexPath.row) {

 73         cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];

 74     }else

 75     {

 76         cell.accessoryView = [[UISwitch alloc] init];

 77     }

 78 //    UIButton *btn = [[UIButton alloc] init];

 79 //    btn.backgroundColor = [UIColor redColor];

 80 //    cell.accessoryView = btn;

 81     

 82     

 83     // 2.4設置cell的背景顏色

 84     cell.backgroundColor = [UIColor blueColor];

 85     

 86     // 設置默認狀態的背景

 87 //    UIView *view = [[UIView alloc] init];

 88 //    view.backgroundColor = [UIColor blueColor];

 89 //    cell.backgroundView = view;

 90     

 91     UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buttondelete"]];

 92     cell.backgroundView = iv;

 93     

 94     // 設置選中狀態的背景

 95     UIView *view2 = [[UIView alloc] init];

 96     view2.backgroundColor = [UIColor purpleColor];

 97     cell.selectedBackgroundView = view2;

 98     // 3.返回cell

 99     return cell;

100 }

101 

102 

103 #pragma mark – 控制狀態欄是否顯示

104 /**

105  *   返回YES代表隱藏狀態欄, NO相反

106  */

107 – (BOOL)prefersStatusBarHidden

108 {

109     return YES;

110 }

111 @end

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *