gpt4 book ai didi

ios - 无法使用 CustomCell 填充 UITableView

转载 作者:行者123 更新时间:2023-11-29 01:24:54 24 4
gpt4 key购买 nike

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

当我在初始页面上选择“进行测试”按钮时,它会转到 UITableView 所在的下一个屏幕,但随后崩溃。

我设置了一个 CustomCell 类,其中包含一个 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,另一张是我的错误的图像。 enter image description here enter image description here

最佳答案

一定要在 Storyboard中为单元格提供重用标识符“QuestionCell”。您还必须向 TableView 注册该标识符的自定义类。 (参见 here)。

在 viewDidLoad 处或之后的某处...

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

关于ios - 无法使用 CustomCell 填充 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34123547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com