简体   繁体   中英

Need help setting up custom cell

I've never implemented a custom cell for a tableView before and am confused on how to proceed. I'm trying to make a personality test app where each question is represented in a cell of my tableView and underneath each question are several buttons that will act as a response to the corresponding question. I've managed to set up my cell with a textview for the question and several buttons with appropriate constraints so they won't move.

I also set the Cell style to custom in the attributes inspector.

Can someone help me with how to set up the textview with my question? I tried making a property of UITextView and linking it to TextView in the cell but then an error arose saying I wasn't allowed to populate a reoccurring cell with an IBOutlet.

Here is a screenshot of my storyboard. Let me know if you need to see anything else. 在此处输入图片说明 在此处输入图片说明

TestVC.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

- (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;
}

CustomCell.h

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

// Variables for questions and answers
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIButton *strongAgree;
@property (weak, nonatomic) IBOutlet UIButton *agree;
@property (weak, nonatomic) IBOutlet UIButton *weakAgree;
@property (weak, nonatomic) IBOutlet UIButton *neutral;
@property (weak, nonatomic) IBOutlet UIButton *strongDisagree;
@property (weak, nonatomic) IBOutlet UIButton *disagree;
@property (weak, nonatomic) IBOutlet UIButton *weakDisagree;

@end

CustomCell.m

#import "CustomCell.h"

@interface CustomCell ()

@end

@implementation CustomCell

@synthesize textView;
@synthesize strongAgree;
@synthesize agree;
@synthesize weakAgree;
@synthesize neutral;
@synthesize strongDisagree;
@synthesize disagree;
@synthesize weakDisagree;

@end

Set a tag for the UITextView in the attributes inspector and get it by the viewWithTag Method:

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

    static NSString *simpleTableIdentifier = @"QuestionCell";

    UITableViewCell *cell = [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];//Use your tag
    textView.text = que;


    return cell;
}

this should work for you.

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

    static NSString *simpleTableIdentifier = @"QuestionCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

    NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
    cell.textLabel.text = que;


    return cell;
}

From what I see here, you doesn't create a custom cell, just a normal UITableViewCell

You need to create a subclass of UITableViewCell. Call it CustomCell or whatever, then give it the IBoutlet properties that you created in your storybard.

For exemple, in CustomCell.h :

@property (weak, nonatomic) IBOutlet UITextView textView;

Then in the cellForRowAtIndexPath :

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

    static NSString *simpleTableIdentifier = @"QuestionCell";

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

    // EDIT
    if (!cell) 
    {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }//

    NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
    cell.textview.text = que;

    return cell;
}

Finally in you storyboard, select the cell you customized, and setup its class in the inspector as CustomCell.

NB : Your IBOutlet properties should always be weak properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM