2013-04-21 68 views
0

我正在嘗試開發一個小應用程序來管理教師的註冊表。我的應用程序的工作原理如下:UITableView在iPad的UIViewController中

  1. 我提出了一個UIViewController(僅適用於景觀)來選擇課程。
  2. 當我按下按鈕(例如:1F)時,我調用一個方法來解析一個XML文件,其中我得到了學生的姓名和姓氏。所以我把這個名字和姓氏放入NSDictionaryNSArrayarrayStudents[0] = dictStudentsdictStudents由3 key: number, name and surname組成)。
  3. 在第二個UIViewController我把一個UITableView放在屏幕的左邊,右邊是一個正常的視圖。在UITableView我想要學生的名字和姓氏,在右邊我想通過點擊UITableView來顯示關於所選學生的考試成績。

這是我viewController看起來像:

viewController UI

我的問題是如何填充UITableView。我將tableViewIBOutlet連接到我的第二個viewController,然後我將代理和數據源連接到第二個viewController,但該應用程序仍然崩潰。我將在這裏發佈我的表視圖類和第二個視圖控制器的代碼,所以你可以幫我解決這個問題。

在這裏你可以看到我tableView的連接選項卡:

connection tab

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

你能不能幫我解決這個問題問題?我做錯了什麼?

回答

1

好吧,所以我對客觀C很新,所以這不是專家意見。但從我得到的,它正在尋找應該在DetailsViewController類中的代理方法。您已將表視圖委託/數據源連接到DetailsViewController類,我認爲您應該指定該類作爲UITableView的委託類。 在DetailsViewController.h:

@interface DetailsViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> 

再插上在DetailsViewController.m文件中的UITableView委託方法。 雖然,我沒有得到的是爲什麼你使用2個表視圖類?如果你想使用的tableView類,爲什麼不在DetailsViewController :: viewDidLoad中,只是初始化:

[self.view addSubView:tableView] 

我不認爲這是在你的類需要tableViewStudentsNameAndSurname。

+0

所以我必須把我的tableView.m的方法放在DetailsViewcontroller.m中? – lucgian841 2013-04-21 08:30:56

+0

它的作品!萬分感謝! – lucgian841 2013-04-21 08:34:26

相關問題