UITableViewCell中的数据源委托

时间:2014-07-03 16:32:38

标签: ios objective-c uitableview delegates

我正在尝试在单元格中实现数据源委托(从TableViewController获取filters数组):

- (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]);
    }
}

问题是delegatenil

在我的TableViewController中我做:

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

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

为什么委托是零?询问代表获取数据的正确位置是什么?

1 个答案:

答案 0 :(得分:2)

您已覆盖setDelegate:而未实际设置代理,这就是为什么它仍为零。如果您覆盖这样的属性设置器,您可能希望以_delegate = delegate启动它以实际分配基础实例变量。那么self.delegate将不再是零。所以:

- (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]);
    }
}