我正在尝试在单元格中实现数据源委托(从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]);
}
}
问题是delegate
是nil
。
在我的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;
}
为什么委托是零?询问代表获取数据的正确位置是什么?
答案 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]);
}
}