簡體   English   中英

使用CustomCell填充UITableView時遇到問題

[英]Trouble populating UITableView with a CustomCell

我正在嘗試制作一個UITableView,每個單元格包含一個問題以及在該問題下方顯示的幾個按鈕,供用戶選擇一個來回答。

當我進入初始頁面並選擇“接受測試”按鈕時,它會顯示到UITableView所在的下一個屏幕,但隨后崩潰。

我設置了一個帶有IBOutlet的CustomCell類,一個UITextView和七個UIButton。 我將UITextView和UIButton鏈接到它們的適當變量。 這是一些代碼,向您展示如何填充每個單元格。 我不了解屏幕截圖中的錯誤。 如果有人可以幫助我解決我做錯的事情,我將不勝感激。 讓我知道是否可以提供更多信息。

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”重用標識符。 您還必須在表視圖中注冊該標識符的定制類。 (請參閱此處 )。

在viewDidLoad或之后的某處...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM