将 UITableView 分成 Objective-C in Xcode11 的部分
Divide UITableView into sections with Objective-C in Xcode11
我是 iOS Xcode 11 和 Objective-C
开发的新手,所以请多多包涵。
我的目标是创建两个页面,其中第一页有一个 按钮 ,按下后会转到第二页。在第二页中,我有一个包含一些数据的简单列表。
我试图将此 TableView
分成几个部分。到目前为止,我已经创建了一个按钮,并且可以 "jump" 从一页到另一页 Segue
。但是在用数据归档列表时遇到了麻烦。我正试图制作这个页面:
但是我得到的是没有任何数据的列表:
这是我的 main.storyboard
:
对于第二个视图,我创建了一个新的 class SecondControllerView.m
并将此 class 附加到第二页
//SecondControllerView.m
#import "SecondViewController.h"
@interface SecondViewController (){
NSMutableArray *numArray;
NSMutableArray *numArray1;
NSMutableArray *numArray2;
NSMutableArray *numArray3;
NSMutableArray *numArray4;
NSMutableArray *numArray5;
NSMutableArray *numArray6;
NSMutableArray *numArray7;
NSMutableArray * sectionArray;
}
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
numArray = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray1 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray2 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray3 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray4 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray5 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray6 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray7 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
sectionArray = [NSMutableArray arrayWithObjects:@"tour 1",@"tour 2",@"tour 3",@"tour 4",@"tour 5",@"tour 6",@"tour 7",@"tour 8",nil];
NSLog(@"Hello world");
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return sectionArray.count;
}
-(NSString * )tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return sectionArray[section];
}
-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
int count = 0;
if(section == 0){
return numArray.count;
}else if (section == 1){
return numArray1.count;
}else if (section == 2){
return numArray2.count;
}else if(section == 3){
return numArray3.count;
}else if(section == 4){
return numArray4.count;
}else if(section == 5){
return numArray5.count;
}else if(section == 6){
return numArray6.count;
}else if(section == 7){
return numArray7.count;
}
return count;
}
-(nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if(indexPath.section == 0){
cell.textLabel.text = numArray[indexPath.row];
}else if(indexPath.section == 1){
cell.textLabel.text = numArray1[indexPath.row];
}else if(indexPath.section == 2){
cell.textLabel.text = numArray2[indexPath.row];
}else if(indexPath.section == 3){
cell.textLabel.text = numArray3[indexPath.row];
}else if(indexPath.section == 4){
cell.textLabel.text = numArray4[indexPath.row];
}else if(indexPath.section == 5){
cell.textLabel.text = numArray5[indexPath.row];
}else if(indexPath.section == 6){
cell.textLabel.text = numArray6[indexPath.row];
}else if(indexPath.section == 7){
cell.textLabel.text = numArray7[indexPath.row];
}
return cell;
}
@end
这里有什么问题吗?
首先,使 SecondViewController
成为 table 视图的委托和数据源 https://guides.codepath.com/ios/Table-View-Guide
其次,在viewDidLoad
的最后做[self.tableView reloadData]
我是 iOS Xcode 11 和 Objective-C
开发的新手,所以请多多包涵。
我的目标是创建两个页面,其中第一页有一个 按钮 ,按下后会转到第二页。在第二页中,我有一个包含一些数据的简单列表。
我试图将此 TableView
分成几个部分。到目前为止,我已经创建了一个按钮,并且可以 "jump" 从一页到另一页 Segue
。但是在用数据归档列表时遇到了麻烦。我正试图制作这个页面:
但是我得到的是没有任何数据的列表:
这是我的 main.storyboard
:
对于第二个视图,我创建了一个新的 class SecondControllerView.m
并将此 class 附加到第二页
//SecondControllerView.m
#import "SecondViewController.h"
@interface SecondViewController (){
NSMutableArray *numArray;
NSMutableArray *numArray1;
NSMutableArray *numArray2;
NSMutableArray *numArray3;
NSMutableArray *numArray4;
NSMutableArray *numArray5;
NSMutableArray *numArray6;
NSMutableArray *numArray7;
NSMutableArray * sectionArray;
}
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
numArray = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray1 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray2 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray3 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray4 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray5 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray6 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
numArray7 = [NSMutableArray arrayWithObjects:@"unit 1",@"unit 2",@"unit 3",@"unit 4",nil];
sectionArray = [NSMutableArray arrayWithObjects:@"tour 1",@"tour 2",@"tour 3",@"tour 4",@"tour 5",@"tour 6",@"tour 7",@"tour 8",nil];
NSLog(@"Hello world");
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return sectionArray.count;
}
-(NSString * )tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return sectionArray[section];
}
-(NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
int count = 0;
if(section == 0){
return numArray.count;
}else if (section == 1){
return numArray1.count;
}else if (section == 2){
return numArray2.count;
}else if(section == 3){
return numArray3.count;
}else if(section == 4){
return numArray4.count;
}else if(section == 5){
return numArray5.count;
}else if(section == 6){
return numArray6.count;
}else if(section == 7){
return numArray7.count;
}
return count;
}
-(nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if(indexPath.section == 0){
cell.textLabel.text = numArray[indexPath.row];
}else if(indexPath.section == 1){
cell.textLabel.text = numArray1[indexPath.row];
}else if(indexPath.section == 2){
cell.textLabel.text = numArray2[indexPath.row];
}else if(indexPath.section == 3){
cell.textLabel.text = numArray3[indexPath.row];
}else if(indexPath.section == 4){
cell.textLabel.text = numArray4[indexPath.row];
}else if(indexPath.section == 5){
cell.textLabel.text = numArray5[indexPath.row];
}else if(indexPath.section == 6){
cell.textLabel.text = numArray6[indexPath.row];
}else if(indexPath.section == 7){
cell.textLabel.text = numArray7[indexPath.row];
}
return cell;
}
@end
这里有什么问题吗?
首先,使 SecondViewController
成为 table 视图的委托和数据源 https://guides.codepath.com/ios/Table-View-Guide
其次,在viewDidLoad
的最后做[self.tableView reloadData]