我正在尝试开发一个小型应用程序来管理老师的注册表。我的应用程序是这样工作的:
- 我提出一个
UIViewController
(只有风景)来选择班级。 - 当我按下按钮(例如:1F)时,我调用一个方法来解析一个
XML
文件,其中我得到了学生的姓名和姓氏。所以我把这个名字和姓氏放到了一个NSArray
of中(NSDictionary
由3组成arrayStudents[0] = dictStudents
dictStudents
key: number, name and surname
)。 - 在第二个中
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
你能帮我解决这个问题吗?我做错了什么?