Я пытаюсь создать UITableView, каждая ячейка которого состоит из вопроса и нескольких кнопок, отображаемых под указанным вопросом, чтобы пользователь мог выбрать один для ответа.
Когда я нахожусь на своей начальной странице и нажимаю кнопку «пройти тест», она переходит к следующему экрану, где находится мой UITableView, но затем происходит сбой.
Я настроил класс CustomCell с IBOutlet для UITextView и семи кнопок UIButton. Я связал UITextView и UIButtons с соответствующими переменными. Вот некоторый код, чтобы показать вам, как я заполняю каждую ячейку. Я не понимаю ошибки на моем скриншоте. Если бы кто-нибудь мог помочь мне с тем, что я делаю неправильно, я был бы очень признателен. Дайте мне знать, если есть дополнительная информация, которую я могу предоставить.
Тествиевконтроллер.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
Тествиевконтроллер.м
#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;
}
Вот два снимка экрана: один из моих UITableView в StoryBoard, а другой — изображение моих ошибок.
cellForRowAtIndexPath
. Убедитесь, что идентификатор повторного использования указан правильно в пользовательской ячейке внутри раскадровки. - person Phillip Mills   schedule 07.12.2015