我试图创建一个UITableView,其中每个单元格都包含一个问题,并且在该问题下面显示的几个按钮可供用户选择一个回答。
当我在我的初始页面上时,我选择按钮'进行测试'它会移动到我的UITableView所在的下一个屏幕,然后崩溃。
我将一个带有IBOutlet的CustomCell类设置为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;
}
答案 0 :(得分:1)
确保在故事板中为单元格提供“QuestionCell”的重用标识符。您还必须使用表视图注册该标识符的自定义类。 (见here)。
在viewDidLoad ...或之后的某处...
[self.tableView registerClass:[CustomCell self]
forCellReuseIdentifier:@"QuestionCell"];