Shmidt
Shmidt

Reputation: 16664

Data source delegate in UITableViewCell

I'm trying to implement data source delegate in cell (to get filters array from TableViewController):

- (void)setDelegate:(id<SLFilterCellDelegate>)delegate{
    NSAssert(self.delegate, @"");
    if ([self.delegate respondsToSelector:@selector(getSLFilters)]) {
        self.filters = [self.delegate getSLFilters];
        self.names = [self.filters valueForKey:@"name"];
        NSLog(@"self.names: %@", [self.names description]);
    }
}

The problem is that delegate is nil.

In my TableViewController I do:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SLFilterCell *cell =
    [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.delegate = self;

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

Why delegate is nil? What is the right place to ask delegate for data?

Upvotes: 0

Views: 45

Answers (1)

Jesse Rusak
Jesse Rusak

Reputation: 57168

You're overriding setDelegate: and not actually setting the delegate, which is why it remains nil. If you override a property setter like that, you probably want to start it with _delegate = delegate to actually assign the underlying instance variable. Then self.delegate will no longer be nil. So:

- (void)setDelegate:(id<SLFilterCellDelegate>)delegate{
    _delegate = delegate;
    if ([self.delegate respondsToSelector:@selector(getSLFilters)]) {
        self.filters = [self.delegate getSLFilters];
        self.names = [self.filters valueForKey:@"name"];
        NSLog(@"self.names: %@", [self.names description]);
    }
}

Upvotes: 2

Related Questions