2012-04-29 132 views
1

看來工作更快做到這一點:把cellforrowatindexpath放在哪裏?

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

問題是,當一個條目被編輯,該小區沒有發生變化。我可以檢查單元格的所有元素,看看它們是否有所不同,但有沒有更好的方法來做到這一點?每次出現單元格時調用drawrect會降低應用程序的速度,似乎沒有必要。

+0

爲什麼編輯問題中的代碼後,在得到答案後,問題的答案如何?哪一個是你的真實代碼?用'drawRect:'或'setNeedsDisplay'? – 2012-04-29 17:58:49

回答

1

如果你想爲你的tableview單元格做自定義繪圖,你必須子類UITableViewCell(因爲它看起來你已經完成了JHomeViewCell)。您的實現JHomeViewCell

@implementation JHomeviewCell 

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

@end 

同樣的內部

看跌的drawRect,你不應該直接調用的drawRect。您應該調用setNeedsDisplay,並可能需要將cellContent.entry值設置爲您的結果控制器中的條目。

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

我把它放在單元格的子視圖裏面,因爲如果我把它作爲單元格的子視圖,它會直接到後面,並且會隱藏在其他單元格內容後面,比如它是背景,這是一個uiview本身。 – Andrew 2012-04-29 15:10:15

+0

這也應該起作用,只要確保將'cellContent.entry'設置爲'entry'並調用'setNeedsDisplay'而不是'drawRect'。 – DHamrick 2012-04-29 15:12:24

+0

謝謝! 'setNeedsDisplay'完美適合我的情況! – Michael 2016-05-30 19:23:13