0

我正在尝试开发一个小型应用程序来管理老师的注册表。我的应用程序是这样工作的:

  1. 我提出一个UIViewController(只有风景)来选择班级。
  2. 当我按下按钮(例如:1F)时,我调用一个方法来解析一个XML文件,其中我得到了学生的姓名和姓氏。所以我把这个名字和姓氏放到了一个NSArrayof中(NSDictionary由3组成arrayStudents[0] = dictStudentsdictStudentskey: number, name and surname)。
  3. 在第二个中UIViewController,我将 aUITableView放在屏幕左侧,将正常视图放在右侧。在UITableView我想要学生的姓名和姓氏中,在右侧我想要显示通过点击 选择的学生的考试分数UITableView

这就是我的viewController样子:

视图控制器界面

我的问题是如何填充UITableView. 我tableView用一个连接IBOutlet到我的第二个viewController,然后我将委托和数据源连接到第二个viewController,但应用程序仍然崩溃。我将在这里发布我的表格视图类和第二个视图控制器的代码,以便您可以帮助我解决这个问题。

在这里您可以看到我的连接选项卡tableView

连接选项卡

表视图.h

#import <UIKit/UIKit.h>

@interface tableView : UITableView <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *arrayStudents;
@end

表视图.m

#import "tableView.h"

@implementation tableView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayStudents.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = [self.arrayStudents[indexPath.row] objectForKey:@"name"];
    cell.detailTextLabel.text = [self.arrayStudents[indexPath.row] objectForKey:@"surname"];

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

@end

详细信息ViewController.h

    #import <UIKit/UIKit.h>

    @interface DetailsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
    @property (weak, nonatomic) IBOutlet UITableView *tableViewStudentsNameAndSurname;
    @property (weak, nonatomic) IBOutlet UIView *viewDetails;
    @property (weak, nonatomic) IBOutlet UILabel *labelName;
    @property (weak, nonatomic) IBOutlet UILabel *labelSurname;
    @property (weak, nonatomic) IBOutlet UITextField *textFieldTest1;
    @property (weak, nonatomic) IBOutlet UITextField *textFieldTest2;
    @property (weak, nonatomic) IBOutlet UITextField *textFieldTest3;
    @property (weak, nonatomic) IBOutlet UITextField *textFieldText4;
    @property (weak, nonatomic) IBOutlet UITextView *textViewValutation;
    @property (weak, nonatomic) IBOutlet UILabel *labelTotalScore;
    @property (nonatomic, strong) NSString *fileName;

    - (IBAction)saveDataToPlistFile:(id)sender;
    @end

详细信息ViewController.m

#import "DetailsViewController.h"
#import "ParserXML.h"
#import "tableView.h"

@interface DetailsViewController () {
    ParserXML *parser;
    tableView *table;
}
@end

@implementation DetailsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    parser = [[ParserXML alloc] init];
    NSLog(@"DetailsViewController: %@", self.fileName);
    parser.fileName = self.fileName;
    [parser parseXML];
    table = [[tableView alloc] init];
    table.arrayStudents = [parser.arrayStudents mutableCopy];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)saveDataToPlistFile:(id)sender {

}
@end

解析器XML.h

#import <Foundation/Foundation.h>

@interface ParserXML : NSObject <NSXMLParserDelegate>
@property (nonatomic, strong) NSMutableArray *arrayStudents;
@property (nonatomic, strong) NSDictionary *dictStudents;
@property (nonatomic, strong) NSString *fileName;

- (void) parseXML;
@end

解析器XML.m

#import "ParserXML.h"

@interface ParserXML() {
    NSXMLParser *nameStudentParser;
    NSString *pathXmlFile;
}
@end

@implementation ParserXML

- (id) init {
    self = [super init];
    if (self) {
        self.arrayStudents = [[NSMutableArray alloc] init];
        pathXmlFile = [[NSBundle mainBundle] pathForResource:self.fileName ofType:@"xml"];
    }
    return self;
}

- (void) parseXML {
    NSURL *xmlUrl = [NSURL URLWithString:pathXmlFile];
    NSString *host = [xmlUrl host];
    if (xmlUrl == nil || host == nil) {
        NSData *data = [[NSData alloc] initWithContentsOfFile:pathXmlFile];
        nameStudentParser = [[NSXMLParser alloc] initWithData:data];
    } else {
        nameStudentParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlUrl];
    }
    [nameStudentParser setDelegate:self];
    [nameStudentParser setShouldProcessNamespaces:NO];
    [nameStudentParser setShouldReportNamespacePrefixes:NO];
    [nameStudentParser setShouldResolveExternalEntities:NO];
    [nameStudentParser parse];
}

- (void)parserDidStartDocument:(NSXMLParser *)parser {
    NSLog(@"File trovato inizio il parsing del documento");
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"Errore Parsing");
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqualToString:@"students"]) {
    } else if ([elementName isEqualToString:@"student"]) {
        NSString *numberValue = attributeDict[@"number"];
        NSString *nameValue = attributeDict[@"name"];
        NSString *surnameValue = attributeDict[@"surname"];

        //Inserire dizionario di array
        self.dictStudents = @{@"number": numberValue,
                                @"name": nameValue,
                            @"surname" : surnameValue};

        [self.arrayStudents addObject:self.dictStudents];
        NSLog(@"arrayStudents dim = %d", self.arrayStudents.count);
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    NSLog(@"ArrayFuels = %@", self.arrayStudents);
}

@end

当我运行应用程序时,Xcode 说:

[DetailsViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 0x751da40

你能帮我解决这个问题吗?我做错了什么?

4

1 回答 1

1

好的,所以我对目标 C 很陌生,所以这不是专家意见。但据我所知,它正在寻找应该存在于 DetailsViewController 类中的委托方法。您已将表视图委托/数据源连接到 DetailsViewController 类,我认为您应该将该类指定为 UITableView 的委托类。在 DetailsViewController.h 中:

@interface DetailsViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

然后在 DetailsViewController.m 文件中插入 UITableView 委托方法。虽然,我不明白你为什么要使用 2 个表视图类?如果你想使用 tableView 类,为什么不在 DetailsViewController::viewDidLoad 中初始化,简单地说:

[self.view addSubView:tableView]  

我认为您的班级不需要 tableViewStudentsNameAndSurname。

于 2013-04-21T08:24:07.797 回答