Andrew
Andrew

Reputation: 16051

Where to put drawrect in cellforrowatindexpath?

It seems to work much faster to do this:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    JHomeViewCell *cell = (JHomeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[JHomeViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];

        cell.cellContent.thumbnailCache = self.thumbnailCache;
    }

    Entry *entry = [self.resultsController objectAtIndexPath:indexPath];
    if (cell.cellContent.entry != entry) {
        [cell.cellContent setNeedsDisplay];
    }
}

The problem is when an entry is edited, the cell doesn't change. I could check for all elements of the cell, to see if they're different, but is there a better way of doing this? Calling drawrect every time the cell appears slows the app down and seems unnecessary.

Upvotes: 1

Views: 280

Answers (1)

DHamrick
DHamrick

Reputation: 8488

If you want to do custom drawing for your tableview cell you have to subclass UITableViewCell (as it looks you have done with JHomeViewCell).

Put drawRect inside of your implementation of JHomeViewCell

@implementation JHomeviewCell

- (void)drawRect:(CGRect)rect
{
    [super drawRect];
    // Insert drawing code here
}

@end

Also, you shouldn't call drawRect directly. You should call setNeedsDisplay instead, and probably need to set the cellContent.entry value to the entry you have from your results controller.

Entry *entry = [self.resultsController objectAtIndexPath:indexPath];
if (cell.cellContent.entry != entry) {
    cell.cellContent.entry = entry;
    [cell setNeedsDisplay];
}

Upvotes: 1

Related Questions