如题所述
iOSå¼åUIç¯âUITableviewæ§ä»¶ç®åä»ç»
ä¸ãåºæ¬ä»ç»
å¨ä¼å¤ç§»å¨åº⽤ç¨ä¸,è½çå°åå¼åæ ·çè¡¨æ ¼æ°æ® ã
å¨iOSä¸,è¦å®ç°è¡¨æ ¼æ°æ®å±ç¤º,æ常ç¨çåæ³å°±æ¯ä½¿ç¨UITableViewï¼UITableView继æ¿èªUIScrollView,å æ¤æ¯æåç´æ»å¨,⽽ä¸æ§è½æä½³ ã
UITableviewæåç»åä¸åç»ä¸¤ç§æ ·å¼ï¼å¯ä»¥å¨storyboardæè æ¯ç¨ä»£ç 设置ã
äºãæ°æ®å±ç¤º
UITableViewéè¦⼀ä¸ä¸ªæ°æ®æº(dataSource)æ¥æ¾ç¤ºæ°æ®
UITableViewä¼åæ°æ®æºæ¥è¯¢ä¸å ±æå¤å°è¡æ°æ®ä»¥åæ¯⼀è¡æ¾ç¤ºä»ä¹æ°æ®ç
没æ设置æ°æ®æºçUITableViewåªæ¯ä¸ªç©ºå£³
å¡æ¯éµå®UITableViewDataSourceåè®®çOC对象,é½å¯ä»¥æ¯UITableViewçæ°æ®æº
å±ç¤ºæ°æ®çè¿ç¨ï¼
ï¼1ï¼è°ç¨æ°æ®æºçä¸é¢⽅æ³å¾ç¥⼀ä¸å ±æå¤å°ç»æ°æ®
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
ï¼2ï¼è°ç¨æ°æ®æºçä¸é¢⽅æ³å¾ç¥æ¯ä¸ç»æå¤å°è¡æ°æ®
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
ï¼3ï¼è°⽤æ°æ®æºçä¸⾯⽅æ³å¾ç¥æ¯⼀⾏æ¾ç¤ºä»ä¹å 容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
ä¸ã代ç 示ä¾
ï¼1ï¼è½åºæ¬å±ç¤ºçâåå¾â代ç
1 #import "NJViewController.h"
2
3 @interface NJViewController ()<UITableViewDataSource>
4 @property (weak, nonatomic) IBOutlet UITableView *tableView;
5
6 @end
7
8 @implementation NJViewController
9
10 - (void)viewDidLoad
11 {
12 [super viewDidLoad];
13 // 设置tableViewçæ°æ®æº
14 self.tableView.dataSource = self;
15
16 }
17
18 #pragma mark - UITableViewDataSource
19 /**
20 * 1.åè¯tableviewä¸å ±æå¤å°ç»
21 */
22 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
23 {
24 NSLog(@"numberOfSectionsInTableView");
25 return 2;
26 }
27 /**
28 * 2.第sectionç»æå¤å°è¡
29 */
30 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
31 {
32 NSLog(@"numberOfRowsInSection %d", section);
33 if (0 == section) {
34 // 第0ç»æå¤å°è¡
35 return 2;
36 }else
37 {
38 // 第1ç»æå¤å°è¡
39 return 3;
40 }
41 }
42 /**
43 * 3.åç¥ç³»ç»æ¯ä¸è¡æ¾ç¤ºä»ä¹å 容
44 */
45 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
46 {
47 NSLog(@"cellForRowAtIndexPath %d %d", indexPath.section, indexPath.row);
48 // indexPath.section; // 第å ç»
49 // indexPath.row; // 第å è¡
50 // 1.å建cell
51 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
52
53 // 2.设置æ°æ®
54 // cell.textLabel.text = @"汽车";
55 // å¤ææ¯ç¬¬å ç»ç第å è¡
56 if (0 == indexPath.section) { // 第0ç»
57 if (0 == indexPath.row) // 第0ç»ç¬¬0è¡
58 {
59 cell.textLabel.text = @"奥迪";
60 }else if (1 == indexPath.row) // 第0ç»ç¬¬1è¡
61 {
62 cell.textLabel.text = @"å®é©¬";
63 }
64
65 }else if (1 == indexPath.section) // 第1ç»
66 {
67 if (0 == indexPath.row) { // 第0ç»ç¬¬0è¡
68 cell.textLabel.text = @"æ¬ç°";
69 }else if (1 == indexPath.row) // 第0ç»ç¬¬1è¡
70 {
71 cell.textLabel.text = @"丰ç°";
72 }else if (2 == indexPath.row) // 第0ç»ç¬¬2è¡
73 {
74 cell.textLabel.text = @"马èªè¾¾";
75 }
76 }
77
78 // 3.è¿åè¦æ¾ç¤ºçæ§ä»¶
79 return cell;
80
81 }
82 /**
83 * 第sectionç»å¤´é¨æ¾ç¤ºä»ä¹æ é¢
84 *
85 */
86 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
87 {
88 // return @"æ é¢";
89 if (0 == section) {
90 return @"å¾·ç³»åç";
91 }else
92 {
93 return @"æ¥é©åç";
94 }
95 }
96 /**
97 * 第sectionç»åºé¨æ¾ç¤ºä»ä¹æ é¢
98 *
99 */
100 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
101 {
102 if (0 == section) {
103 return @"é«ç«¯å¤§æ°ä¸æ¡£æ¬¡";
104 }else
105 {
106 return @"è¿ä¸é";
107 }
108 }
109 @end
ä¸ãåºæ¬ä»ç»
å¨ä¼å¤ç§»å¨åº⽤ç¨ä¸,è½çå°åå¼åæ ·çè¡¨æ ¼æ°æ® ã
å¨iOSä¸,è¦å®ç°è¡¨æ ¼æ°æ®å±ç¤º,æ常ç¨çåæ³å°±æ¯ä½¿ç¨UITableViewï¼UITableView继æ¿èªUIScrollView,å æ¤æ¯æåç´æ»å¨,⽽ä¸æ§è½æä½³ ã
UITableviewæåç»åä¸åç»ä¸¤ç§æ ·å¼ï¼å¯ä»¥å¨storyboardæè æ¯ç¨ä»£ç 设置ã
äºãæ°æ®å±ç¤º
UITableViewéè¦⼀ä¸ä¸ªæ°æ®æº(dataSource)æ¥æ¾ç¤ºæ°æ®
UITableViewä¼åæ°æ®æºæ¥è¯¢ä¸å ±æå¤å°è¡æ°æ®ä»¥åæ¯⼀è¡æ¾ç¤ºä»ä¹æ°æ®ç
没æ设置æ°æ®æºçUITableViewåªæ¯ä¸ªç©ºå£³
å¡æ¯éµå®UITableViewDataSourceåè®®çOC对象,é½å¯ä»¥æ¯UITableViewçæ°æ®æº
å±ç¤ºæ°æ®çè¿ç¨ï¼
ï¼1ï¼è°ç¨æ°æ®æºçä¸é¢⽅æ³å¾ç¥⼀ä¸å ±æå¤å°ç»æ°æ®
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
ï¼2ï¼è°ç¨æ°æ®æºçä¸é¢⽅æ³å¾ç¥æ¯ä¸ç»æå¤å°è¡æ°æ®
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
ï¼3ï¼è°⽤æ°æ®æºçä¸⾯⽅æ³å¾ç¥æ¯⼀⾏æ¾ç¤ºä»ä¹å 容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
ä¸ã代ç 示ä¾
ï¼1ï¼è½åºæ¬å±ç¤ºçâåå¾â代ç
1 #import "NJViewController.h"
2
3 @interface NJViewController ()<UITableViewDataSource>
4 @property (weak, nonatomic) IBOutlet UITableView *tableView;
5
6 @end
7
8 @implementation NJViewController
9
10 - (void)viewDidLoad
11 {
12 [super viewDidLoad];
13 // 设置tableViewçæ°æ®æº
14 self.tableView.dataSource = self;
15
16 }
17
18 #pragma mark - UITableViewDataSource
19 /**
20 * 1.åè¯tableviewä¸å ±æå¤å°ç»
21 */
22 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
23 {
24 NSLog(@"numberOfSectionsInTableView");
25 return 2;
26 }
27 /**
28 * 2.第sectionç»æå¤å°è¡
29 */
30 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
31 {
32 NSLog(@"numberOfRowsInSection %d", section);
33 if (0 == section) {
34 // 第0ç»æå¤å°è¡
35 return 2;
36 }else
37 {
38 // 第1ç»æå¤å°è¡
39 return 3;
40 }
41 }
42 /**
43 * 3.åç¥ç³»ç»æ¯ä¸è¡æ¾ç¤ºä»ä¹å 容
44 */
45 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
46 {
47 NSLog(@"cellForRowAtIndexPath %d %d", indexPath.section, indexPath.row);
48 // indexPath.section; // 第å ç»
49 // indexPath.row; // 第å è¡
50 // 1.å建cell
51 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
52
53 // 2.设置æ°æ®
54 // cell.textLabel.text = @"汽车";
55 // å¤ææ¯ç¬¬å ç»ç第å è¡
56 if (0 == indexPath.section) { // 第0ç»
57 if (0 == indexPath.row) // 第0ç»ç¬¬0è¡
58 {
59 cell.textLabel.text = @"奥迪";
60 }else if (1 == indexPath.row) // 第0ç»ç¬¬1è¡
61 {
62 cell.textLabel.text = @"å®é©¬";
63 }
64
65 }else if (1 == indexPath.section) // 第1ç»
66 {
67 if (0 == indexPath.row) { // 第0ç»ç¬¬0è¡
68 cell.textLabel.text = @"æ¬ç°";
69 }else if (1 == indexPath.row) // 第0ç»ç¬¬1è¡
70 {
71 cell.textLabel.text = @"丰ç°";
72 }else if (2 == indexPath.row) // 第0ç»ç¬¬2è¡
73 {
74 cell.textLabel.text = @"马èªè¾¾";
75 }
76 }
77
78 // 3.è¿åè¦æ¾ç¤ºçæ§ä»¶
79 return cell;
80
81 }
82 /**
83 * 第sectionç»å¤´é¨æ¾ç¤ºä»ä¹æ é¢
84 *
85 */
86 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
87 {
88 // return @"æ é¢";
89 if (0 == section) {
90 return @"å¾·ç³»åç";
91 }else
92 {
93 return @"æ¥é©åç";
94 }
95 }
96 /**
97 * 第sectionç»åºé¨æ¾ç¤ºä»ä¹æ é¢
98 *
99 */
100 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
101 {
102 if (0 == section) {
103 return @"é«ç«¯å¤§æ°ä¸æ¡£æ¬¡";
104 }else
105 {
106 return @"è¿ä¸é";
107 }
108 }
109 @end
温馨提示:答案为网友推荐,仅供参考