【问题标题】:Custom UITableViewCell using storyboard and subclass使用故事板和子类自定义 UITableViewCell
【发布时间】:2013-11-29 10:55:08
【问题描述】:

我创建了一个UITableViewCell 的子类,我称之为DCCustomCell。这是DCCustomCell.h 文件。

#import <UIKit/UIKit.h>

@interface DCCustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *date;
@property (weak, nonatomic) IBOutlet UILabel *price;

@end

所有的属性都与我的iPhone.storyboard 文件中的元素正确连接。 我还在iPhone.storyboard 中选择了tableViewCell,并将单元格的标识符设置为“CustomCell”,将类设置为DCCustomCell

这是 UITableViewController 的 viewDidLoad 方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

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

    // Do any additional setup after loading the view, typically from a nib.
}

这是 tableView:cellForRowAtIndexPath: 方法:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"CustomCell";

    DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];

    NSLog(@"%@" ,cell);

    cell.imageView.image = [UIImage imageNamed:@"info_button.png"];
    cell.title.text = @"Hello!";
    cell.date.text = @"21/12/2013";
    cell.price.text = @"€15.00";

    return cell;
}

问题是imageView设置正确,但是所有标签都是空白的。如果我将 imageView 属性名称更改为例如 eventImageView,则不会设置图像。 结果如下:

我不知道如何解决这个问题。

编辑:如果我删除

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

来自viewDidLoad 似乎一切正常。为什么?

【问题讨论】:

    标签: ios uitableview storyboard subclass


    【解决方案1】:

    正如您注意到的那样,当您删除它时它会起作用

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

    仅当您使用 dequeueReusableCellWithIdentifier:indexPath: 并且尚未在该单元格的身份检查器中设置自定义类时,您才需要这样做。如果您使用registerClass: forCellReuseIdentifier,则单元格将被初始化为initWithStyle:reuseIdentifier: 而不是initWithCoder:,从而破坏了您在情节提要中建立的连接。

    【讨论】:

      【解决方案2】:

      您是否将控件绑定到 IBOutlet ? 您可以尝试直接在 Storyboard 中将 DCCustomCell 设置为 UITableviewCell 的类。 而且你需要在单元格中设置带有控件的插座

      【讨论】:

        【解决方案3】:

        试试:

        if (cell == null) {
            //create and initialize cell
        }
        

        之后

        DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
        

        【讨论】:

          【解决方案4】:

          您首先正确检查您的网点,确保所有标签都已连接并写下:

              - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
              {
                  static NSString *identifier = @"CustomCell";
          
                  DCCustomCell *cell = (DCCustomCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
          
                  if (cell == nil)
                  {
                      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DCCustomCell" owner:self options:nil];
          
                      cell = [nib objectAtIndex:0];
                  }
                  NSLog(@"%@" ,cell);
          
                  cell.imageView.image = [UIImage imageNamed:@"info_button.png"];
          
                  cell.title.text = @"Hello!";
          
                  cell.date.text = @"21/12/2013";
          
                  cell.price.text = @"€15.00";
          
                  return cell;
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-14
            • 1970-01-01
            相关资源
            最近更新 更多