【问题标题】:Need help setting up custom cell需要帮助设置自定义单元格
【发布时间】:2015-12-03 13:34:10
【问题描述】:

我以前从未为 tableView 实现过自定义单元格,并且对如何继续感到困惑。我正在尝试制作一个个性测试应用程序,其中每个问题都表示在我的 tableView 的一个单元格中,并且每个问题下方都有几个按钮,它们将作为对相应问题的响应。我已经设法为我的单元格设置了问题的文本视图和几个具有适当约束的按钮,因此它们不会移动。

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

有人可以帮我解决我的问题如何设置文本视图吗?我尝试创建 UITextView 的属性并将其链接到单元格中的 TextView,但随后出现错误,提示我不允许使用 IBOutlet 填充重复出现的单元格。

这是我的故事板的屏幕截图。如果您需要查看其他内容,请告诉我。

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

【问题讨论】:

    标签: ios objective-c iphone uitableview custom-cell


    【解决方案1】:

    在属性检查器中为 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;
    }
    

    这应该适合你。

    【讨论】:

      【解决方案2】:
      - (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

      【讨论】:

        【解决方案3】:

        你需要创建一个 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 属性应该始终是弱属性。

        【讨论】:

        • 我听从了您的建议,但遇到了错误。我发布了我的错误截图并更新了代码
        • 你知道我做错了什么吗?我不明白错误信息
        • 更新了我的答案,如果它解决了您的问题,请告诉我
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-13
        • 2010-12-08
        相关资源
        最近更新 更多