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

标签 ios objective-c uitableview xcode11

我是使用 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/

相关文章:

ios - 单击完成按钮时调用 UITextField 方法

objective-c - 如何设置 kAudioUnitSubType_Distortion

objective-c - 为什么我不在传递给 dispatch_after() 的 block 中使用指向自身的弱指针?

ios - 在 tableviewcontroller 中缓存图像

ios - 如何将 UITextView 添加到 UITableView 详细信息

ios - CAAnimationGroup 中的 CAAnimation 委托(delegate)

ios - 在两个不同的类中实现 UITableViewDataSource 协议(protocol)

iphone - 如何更改Tapku日历的背景图像

ios - Objective-C:像 2014-02-02T20:55:31.555Z 这样的日期的日期格式是什么

ios - Swifty JSON UITableView 后台报错