简体   繁体   中英

How can I pass an object from fetchedResultsController to a custom cell?

I want to send an object, from a fetchedResultsController, to a custom cell.

- (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;
// ...
}

And from my Custom Cell's .h and .m file

.h

@property (strong, nonatomic) Work *workInfo;

.m

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

_workInfo, is returning nil! What am I missign here? How can I pass an object to my custom cell?

I can perfectly set up a text label, but not send an object from my FRC?

Thank you!

awakeFromNib happens before you set workInfo (and it isn't going to be called at all if the cell is being reused). The normal thing to do here is write a custom property setter for workInfo like

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

and make any Work-related updates to the cell inside the setter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM