我正在嘗試開發一個小應用程序來管理教師的註冊表。我的應用程序的工作原理如下:UITableView在iPad的UIViewController中
- 我提出了一個
UIViewController
(僅適用於景觀)來選擇課程。 - 當我按下按鈕(例如:1F)時,我調用一個方法來解析一個
XML
文件,其中我得到了學生的姓名和姓氏。所以我把這個名字和姓氏放入NSDictionary
的NSArray
(arrayStudents[0] = dictStudents
和dictStudents
由3key: number, name and surname
組成)。 - 在第二個
UIViewController
我把一個UITableView
放在屏幕的左邊,右邊是一個正常的視圖。在UITableView
我想要學生的名字和姓氏,在右邊我想通過點擊UITableView
來顯示關於所選學生的考試成績。
這是我viewController
看起來像:
我的問題是如何填充UITableView
。我將tableView
與IBOutlet
連接到我的第二個viewController
,然後我將代理和數據源連接到第二個viewController
,但該應用程序仍然崩潰。我將在這裏發佈我的表視圖類和第二個視圖控制器的代碼,所以你可以幫我解決這個問題。
在這裏你可以看到我tableView
的連接選項卡:
tableView.h
#import <UIKit/UIKit.h>
@interface tableView : UITableView <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *arrayStudents;
@end
tableView.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
detailsViewController.h
detailsViewController.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
ParserXML.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
ParserXML。米
#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
你能不能幫我解決這個問題問題?我做錯了什麼?
所以我必須把我的tableView.m的方法放在DetailsViewcontroller.m中? – lucgian841 2013-04-21 08:30:56
它的作品!萬分感謝! – lucgian841 2013-04-21 08:34:26