2015-12-03 110 views
0

我从来没有为tableView实现自定义单元格,并且对如何继续操作感到困惑。我试图制作一个个性测试应用程序,其中每个问题都在我的tableView的一个单元格中进行表示,每个问题下面有几个按钮,这些按钮将作为对相应问题的回应。我已经成功设置了我的单元格的问题和几个按钮与适当的约束,所以他们不会移动textview。需要帮助设置自定义单元格

我还在属性检查器中将单元格样式设置为自定义。

有人可以帮助我如何设置与我的问题的TextView?我试着制作UITextView的一个属性并将它链接到单元格中的TextView,但随后出现一个错误,说我不允许用IBOutlet填充一个重复出现的单元格。

这是我的故事板的屏幕截图。让我知道你是否需要看别的东西。 enter image description here enter image description here

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 

回答

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

从我看到这里,你不创建一个自定义单元格,只是一个普通的UITableViewCell

0

您需要创建的UITableViewCell的子类。把它叫做CustomCell或其他什么,然后给它在你的故事中创建的IBoutlet属性。

对于为例,在CustomCell.h:

@property (weak, nonatomic) IBOutlet UITextView textView; 

然后在的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; 
} 

最后,在你的故事板,选择单元格你定制的,并设置同级车中检查作为CustomCell。

注意:您的IBOutlet属性应始终属性较弱。

+0

我按照你的建议,但我遇到了错误。我发布了我的错误截图并更新了代码 – Talcicio

+0

你知道我在做什么错吗?我不明白错误消息 – Talcicio

+0

更新了我的答案,请告诉我它是否修复了您的问题 – Kujey

1

设置由viewWithTag法的UITextView的在属性检查器标签,并得到它:

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

这应该为你工作。