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

时间:2017-08-01 07:05:39

标签: ios

我想构建一个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

我将tag的值设置为1。

2 个答案:

答案 0 :(得分:0)

您可能需要像

一样进行投射
UITableView *tableView = (UITableView *)[self.view viewWithTag:1];

答案 1 :(得分:0)

<?php
$conn=mysqli_connect("localhost","root","","db_name");
$query="SELECT table_name FROM information_schema.tables where table_schema='db_name';";

$result=mysqli_query($conn,$query);
$tables=array();
while ($row=mysqli_fetch_array($result)) {
    $query="RENAME TABLE ".$row[0]." TO ".$row[0]."_city";
    mysqli_query($conn,$query);
}

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

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

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

如果您在故事板中使用原型单元,则不需要注册类。