почему метод init в ячейке не вызывается, когда происходит dequeueReusableCellWithIdentifier - PullRequest
0 голосов
/ 11 ноября 2019

мой код выглядит следующим образом

viewcontroller.m

- (void)viewDidLoad {
    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] init];

    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = @"Hello, world";
    cell.delegate = self;
    return cell;
}

cell.m

- (void)awakeFromNib {
    [super awakeFromNib];
    [self commonInit];
    // Initialization code
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress)];
    [self addGestureRecognizer:longPress];
}


- (void)longPress
{
    [self.delegate actionTriggered:self];
}

, поэтому на экране отображается табличное представление, содержащее только одну ячейку "приветмир "при использовании длинного нажатия жеста ничего не произошло.

, поэтому я установил точку останова в commonInit. выяснить, этот метод не вызывается. не могу понять почему. и как это исправить. ценим любую помощь.

1 Ответ

0 голосов
/ 11 ноября 2019

В 'viewDidLoad' вы создаете локальную переменную 'tableView'. Эта переменная выходит из области видимости, как только метод завершается.

Это не может работать.

Предполагая, что класс в вашем файле viewcontroller.m наследуется от UITableViewController, который вы просто должны сделатьубедитесь, что вы настроили существующее свойство tableView вместо создания локальной переменной:

-(void)viewDidLoad {
    [super viewDidLoad];

    // UITableView *tableView = [[UITableView alloc] init];  <-- Don't!

    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...