2012-09-01 172 views
4

關於刷新自定義表格單元格有許多問題,但這個很奇怪。我有一個tableView,每個單元格現在都有一個自定義的UILabelUIImageViewUITableView自定義單元格不刷新

目前只有2個電池。第一個顯示日期。當表第一次加載時,它將當前日期顯示爲第一個單元格中的字符串UILabel

當我選擇所述第一小區,我提出了一種自定義類負責處理所有的時間採摘。一旦挑選了一個日期,這個視圖就會彈出,然後我返回到表格視圖。

-(void)viewDidAppear的tableviews數據被重載,新選定的日期應該會出現。

但是,第一個單元格中的標籤未更新。

什麼迷惑的事,如果我有多個小區都顯示相同的數據,這些都將刷新並顯示新的日期,符合市場預期。看起來索引爲0的單元格行不會刷新。

什麼進一步混淆了事情是,當我查詢UILabel的字符串值單元格,則返回正確的日期。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     // 
     // clears the grouped style border from each table cell 
     // 

     UIView *clearBgView = [[UIView alloc]initWithFrame:CGRectZero]; 
     [cell setBackgroundView:clearBgView]; 

     // label 
     UILabel *dl = [[UILabel alloc]initWithFrame:CGRectMake(70.0f, 10.0f, screenWidth-100, 50)]; 
     [self setDetailsLabel:dl]; 
     dl = nil; 

     [[self detailsLabel] setBackgroundColor:[UIColor colorWithRed:.1 green:.1 blue:.1 alpha:.1 ]]; 
     [[self detailsLabel] setTextColor:[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:.3f]]; 

     //icon for each cell 
     UIImageView *ci = [[UIImageView alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 50.0f, 50.0f)]; 
     [ci setBackgroundColor:[UIColor colorWithRed:.2 green:.2 blue:.2 alpha:.2]]; 
     [self setCellIcon:ci]; 
     ci = nil; 

     // 
     // set up views 
     // 

     [cell addSubview:[self cellIcon]]; 
     [cell addSubview:[self detailsLabel]]; 
    } 

    // Configure the cell... 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 



    //populate each by row. 
    NSString *dateDisplay = [self formatDate:[self dateCaught]]; 
    NSLog (@"date is %@", dateDisplay); 
    [[self detailsLabel] setText:[self formatDate:[self dateCaught]]]; 

    switch (indexPath.row) { //this needs to be an integer, so return the row of the indexPath. 
     case 0: 
      NSLog (@"text for the cell is %@",[[self detailsLabel]text]); 
      break; 

     default: 
      break; 
    } 


return cell; 

}

回答

4

的問題與您通過detailsLabel周圍的方式做。你把它放在一個屬性或伊娃在self

[self setDetailsLabel:dl]; 

但你設置它,只有當細胞是可以重複使用。當您重複使用該單元格時,self上的detailsLabel被設置爲之前運行的標籤,從而導致您遇到各種問題。

最簡潔的解決方案是創建自己的類,派生自UITableViewCell,將創建標籤,圖標,背景顏色等的初始化代碼移動到指定的初始化程序中,並創建用於設置標籤文本的屬性。有了這個類的地方,你就可以簡化您的代碼如下:

UIMyCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UIMyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
[[cell detailsLabel] setText:[self formatDate:[self dateCaught]]]; 
// ^--- detailsLabel can be moved to UIMyCustomTableViewCell now 
+0

感謝您最近的更新,這正是我要做的。我試圖通過像我這樣快速定製單元格內容來嘗試破解它,但正如你所看到的,遇到了問題。 – Tim

0

解決的辦法是,不使用一個成員變量一樣@property detailsLabel,而是標籤爲所有自定義子視圖。改變你的代碼是這樣的:

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

      // label 
      UILabel *dl = [[UILabel alloc]initWithFrame:...]; 
      dl.tag = 99; 
      [cell.contentView addSubview: dl]; 

      [...] 
     } 

     [...] 

     UILabel *dl = [cell.contentView viewWithTag: 99]; 
     [dl setText:[self formatDate:[self dateCaught]]]; 

     [...] 
     return cell; 
}