[英]Need help setting up custom cell
I've never implemented a custom cell for a tableView before and am confused on how to proceed. 我以前从未为tableView实现自定义单元,并且对如何继续感到困惑。 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.
我正在尝试制作一个个性测试应用程序,其中每个问题都在tableView的单元格中表示,并且每个问题下方是几个按钮,这些按钮将用作对相应问题的响应。 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.
我尝试制作UITextView的属性并将其链接到单元格中的TextView,但是随后出现一个错误,提示我不允许使用IBOutlet填充重复出现的单元格。
Here is a screenshot of my storyboard. 这是我的故事板的屏幕截图。 Let me know if you need to see anything else.
让我知道您是否还有其他需要。
TestVC.h 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 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 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 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: 在属性检查器中为UITextView设置标签,并通过viewWithTag方法获取它:
- (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
根据我在这里看到的内容,您不会创建自定义单元,而只是创建普通的
UITableViewCell
You need to create a subclass of UITableViewCell. 您需要创建UITableViewCell的子类。 Call it CustomCell or whatever, then give it the IBoutlet properties that you created in your storybard.
将其命名为CustomCell或其他名称,然后为其赋予您在故事栏中创建的IBoutlet属性。
For exemple, in CustomCell.h : 例如,在CustomCell.h中:
@property (weak, nonatomic) IBOutlet UITextView textView;
Then in the cellForRowAtIndexPath : 然后在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. 最后,在情节提要中,选择您自定义的单元格,然后在检查器中将其类设置为CustomCell。
NB : Your IBOutlet properties should always be weak properties. 注意:您的IBOutlet属性应该始终是弱属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.