无法使用 CustomCell 填充 UITableView

Trouble populating UITableView with a CustomCell

我正在尝试制作一个 UITableView,其中每个单元格都包含一个问题,并且显示在所述问题下方的几个按钮供用户选择一个来回答。

当我在我的初始页面上时,我 select 按钮 'take test' 它转到我的 UITableView 所在的下一个屏幕,但随后崩溃。

我设置了一个 CustomCell class,带有一个 IBOutlet 到一个 UITextView 和七个 UIButton。我将 UITextView 和 UIButtons 链接到它们适当的变量。下面是一些代码,向您展示我如何填充每个单元格。我不明白我的屏幕截图中的错误。如果有人可以帮助我解决我做错的事情,我将不胜感激。如果我可以提供更多信息,请告诉我。

TestViewController.h

#import <UIKit/UIKit.h>
#import "Questions.h"
#import "CustomCell.h"

@interface TestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UITextView *instructions;
@property (weak, nonatomic) IBOutlet UIButton *results;
//@property (strong, nonatomic) IBOutlet *question;
@property (nonatomic, strong) Questions *survey;

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

TestViewController.m

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController {
    NSDictionary *allQuestions;
}

@synthesize survey;
@synthesize results;
@synthesize instructions;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"Personality Test";
    survey = [[Questions alloc] init];

    // set up question array
    survey.questions = @[@"1. You find it difficult to introduce yourself to other people",
                        @"2. You often get so lost in thoughts that you ignore or forget your surroundings",
                        // many more.....
                       ];

    // set up answers array
    // -1 means unanswered
    // 0 means neutral
    // 1 means weakly agree or weakly disagree
    // 2 means agree or disagree
    // 3 means strongly agree or strongly disagree
    survey.answers = @[[NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       // many more.....
                      ];

    // set up dictionary of question/answer relationship
    allQuestions = [NSDictionary dictionaryWithObjects:survey.questions forKeys:survey.answers];

    //
    // depending on what question is asked based on categories
    // each question fits into one of the 5:
    // 1. Mind: Introvert or Extravert
    // 2. Energy: Observant or Intuitive
    // 3. Nature: Thinking or Feeling
    // 4. Tactics: Judging or Prospecting
    // 5. Identity: Assertive or Turbulent
    // Each array spot will hold one of the two words on the right

    // set up types array
    survey.types = @[@"neutral",
                     @"neutral",
                    // many more.....
                   ];

    NSString *instr = @"Things to know:\n1. Takes less than 12 minutes.\n2. Answer honestly, even if you don't like the answer. \n3. Try not to leave any 'neutral' answers ";
    //UIFont *instrFont = [UIFont fontWithName:@"HelveticaNeue" size:12];
    //CGSize textviewSize = [instr sizeWithFont:instrFont constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    //instructions = [[UITextView alloc] initWithFrame:CGRectMake(100, 30, 300, 100)];
    instructions.text = instr;
    //[self.view addSubview:instructions];

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(backPressed:)];
    self.navigationItem.leftBarButtonItem = backButton;
    self.navigationItem.hidesBackButton = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [survey.questions count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"QuestionCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    /*
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    */

    NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
    //UITextView *textView = (UITextView *)[cell viewWithTag: 100];
    cell.textView = (UITextView *)[cell viewWithTag: 100];
    cell.textView.text = que;

    return cell;
}

这里有两个屏幕截图:一个是 StoryBoard 中我的 UITableView,另一个是我的错误图像。

确保在故事板中为单元格提供重用标识符 "QuestionCell"。您还必须使用 table 视图为该标识符注册自定义 class。 (参见 here)。

viewDidLoad 处或之后的某处...

[self.tableView registerClass:[CustomCell self]
       forCellReuseIdentifier:@"QuestionCell"];