繁体   English   中英

[UIView registerClass:forCellReuseIdentifier:]:无法识别的选择器已发送到实例

[英][UIView registerClass:forCellReuseIdentifier:]: unrecognized selector sent to instance

我想建立一个tableView单元格并向其添加子视图。但是那里有些问题。标题是错误,语言是Objective-c,请帮帮我,谢谢! 这是UITableViewCell的子类,名称为NameAndColorCell。

NameAndColorCell.h:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface NameAndColorCell : UITableViewCell

@property (copy,nonatomic) NSString *name;
@property (copy,nonatomic) NSString *color;

@end

NameAndColorCell.m:

 #import "NameAndColorCell.h"

@interface NameAndColorCell()

@property (strong,nonatomic) UILabel *nameLabel;
@property (strong,nonatomic) UILabel *colorLabel;

@end

@implementation NameAndColorCell

- (id)initWithStyle : (UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CGRect nameLabelRect = CGRectMake(0, 5, 70, 15);
        UILabel *nameMaker = [[UILabel alloc]initWithFrame:nameLabelRect];
        nameMaker.textAlignment = NSTextAlignmentRight;
        nameMaker.text = @"Name";
        nameMaker.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview:nameMaker];

        CGRect colorLabelRect = CGRectMake(0, 26, 70, 15);
        UILabel *colorMaker = [[UILabel alloc]initWithFrame:colorLabelRect];
        colorMaker.textAlignment = NSTextAlignmentRight;
        colorMaker.text = @"color";
        colorMaker.font = [UIFont boldSystemFontOfSize:12];
        [self.contentView addSubview:colorMaker];

        CGRect nameValueRect = CGRectMake(80, 5, 200, 15);
        _nameLabel = [[UILabel alloc]initWithFrame:nameValueRect];
        [self.contentView addSubview:_nameLabel];

        CGRect colorValueRect = CGRectMake(80, 25, 200, 15);
        _colorLabel = [[UILabel alloc]initWithFrame:colorValueRect];
        [self.contentView addSubview:_colorLabel];
    }
    return self;
}

- (void)setName:(NSString *)n{
    if (![n isEqualToString:_name]) {
        _name = [n copy];
        self.nameLabel.text = _name;
    }
}

- (void)setColor:(NSString *)c{
    if (![c isEqualToString:_color]) {
        _color = [c copy];
        self.colorLabel.text = _color;
    }
}

@end

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

<UITableViewDataSource,UITableViewDelegate>

@end

ViewController.m:

#import "ViewController.h"
#import "NameAndColorCell.h"

static NSString *CellTableIdentifier = @"CellTableIdentifier";
@interface ViewController ()

@property (copy,nonatomic) NSArray *computer;

@end

@implementation ViewController

 - (void)viewDidLoad {
    [super viewDidLoad];
    self.computer = @[@{@"Name" : @"MacBook Air", @"Color" : @"Silver"},
                      @{@"Name" : @"MacBook Pro", @"Color" : @"Silver"},
                      @{@"Name" : @"iMac", @"Color" : @"Silver"},
                      @{@"Name" : @"MacMini", @"Color" : @"Silver"},
                      @{@"Name" : @"Mac Pro", @"Color" : @"Black"}];

    UITableView *tableView = (id)[self.view viewWithTag:1];
    [tableView registerClass:[NameAndColorCell class] forCellReuseIdentifier:CellTableIdentifier];

    UIEdgeInsets contentInset = tableView.contentInset;
    contentInset.top = 20;
    [tableView setContentInset:contentInset];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.computer count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NameAndColorCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier forIndexPath:indexPath];
    NSDictionary *rowData = self.computer[indexPath.row];
    cell.name = rowData[@"Name"];
    cell.color = rowData[@"Color"];

    return cell;
}

@end

我将标签的值设置为1。

您可能需要像

UITableView *tableView = (UITableView *)[self.view viewWithTag:1];
`UITableView *tableView = (id)[self.view viewWithTag:1]`  

返回UIView而不是UITableView。 您需要将其正确转换为UITableView。 你应该用

UITableView *tableView = (UITableView *)[self.view viewWithTag:1];

可以使用其创建IBOutlet,而不是使用其标签访问TableView。

如果在情节提要板中使用原型单元,则不需要registerClass。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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