如何将fetchedResultsController中的对象传递给自定义单元格?

时间:2013-07-24 14:37:13

标签: ios objective-c uitableview

我想将一个对象从fetchedResultsController发送到自定义单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
        TWMainViewExpandedCell *cell = (TWMainViewExpandedCell *)[tableView dequeueReusableCellWithIdentifier:StandardExpandedCellIdentifier];

        if (cell == nil) {
            cell = (TWMainViewExpandedCell *)[[[NSBundle mainBundle] loadNibNamed:@"MainViewStandardCellExpanded" owner:self options:nil] objectAtIndex:0];
        }

        Work *work = [_fetchedResultsController objectAtIndexPath:indexPath];
        cell.workInfo = work; // <- NSLog work.description confirms object exists!
        return cell;
// ...
}

来自我的Custom Cell的.h和.m文件

·H

@property (strong, nonatomic) Work *workInfo;

的.m

- (void) awakeFromNib {
    // ...
    NSLog(@"%@", _workInfo); // <- why is this nil?
    // ...
}

_workInfo,正在返回零!我在这里错过了什么?如何将对象传递给自定义单元格?

我可以完美地设置文本标签,但不能从我的FRC发送对象吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

awakeFromNib在您设置workInfo之前发生(如果重用该单元格,则根本不会调用它)。这里正常的做法是为workInfo编写一个自定义属性设置器,如

- (void)setWorkInfo:(Work *)work
{
    if (_work != work) {
        _work = work;
        //make any Work-related updates to the cell here
    }
}

并对setter中的单元格进行任何与Work相关的更新。