gpt4 book ai didi

ios - 在 Xcode11 中用 Objective-C 将 UITableView 分成几个部分

转载 作者:行者123 更新时间:2023-11-28 20:51:06 25 4
gpt4 key购买 nike

我是使用 Objective-CXcode 11 中进行 iOS 开发的新手,所以请多多包涵。

我的目标是创建两个页面,其中第一页有一个按钮,按下后会转到第二页。在第二页中,我有一个包含一些数据的简单列表。

我试图将此 TableView 分成几个部分。到目前为止,我已经创建了一个按钮,可以使用 Segue 从一个页面“跳转”到另一个页面。但是在用数据归档列表时遇到了麻烦。我正试图制作这个页面:
Correct Output

但是我得到的是没有任何数据的列表:
Error output

这是我的main.storyboard:
Page structure

对于第二个 View ,我创建了一个新类 SecondControllerView.m 并将这个类附加到第二个页面
attaching class to the View

  //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成为 TableView 的委托(delegate)和数据源https://guides.codepath.com/ios/Table-View-Guide

其次,在viewDidLoad的最后做[self.tableView reloadData]

关于ios - 在 Xcode11 中用 Objective-C 将 UITableView 分成几个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58271235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com