【问题标题】:UITableView in a UIViewController on an iPadiPad 上 UIViewController 中的 UITableView
【发布时间】:2013-04-21 08:01:23
【问题描述】:

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

  1. 我提交UIViewController(仅限横向)来选择课程。
  2. 当我按下按钮时(例如:1F),我调用一个方法来解析一个XML 文件,其中我得到了学生的姓名和姓氏。所以我把这个名字和姓氏放到了NSDictionary的一个NSArray中(arrayStudents[0] = dictStudentsdictStudents是由3个key: number, name and surname组成)。
  3. 在第二个UIViewController 中,我在屏幕左侧放了一个UITableView,在右侧放了一个普通视图。在UITableView 我想显示学生的姓名和姓氏,在右侧我想通过点击UITableView 显示所选学生的考试成绩。

这就是我的viewController 的样子:

我的问题是如何填充UITableView。我将tableViewIBOutlet 连接到我的第二个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

    #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

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.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

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

【问题讨论】:

    标签: objective-c ipad uitableview ios6


    【解决方案1】:

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

    @interface DetailsViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
    

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

    [self.view addSubView:tableView]  
    

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

    【讨论】:

    • 所以我要把tableView.m的方法放到DetailsViewcontroller.m中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多