2012-08-27 38 views
1

我有一個UITableView與幾個自定義單元格,其中兩個包含圖像瀏覽。當它們被填充圖像時,它們很好,但是如果我向下滾動tableview,以便圖像視圖不在視圖中,然後返回,則圖像消失,從而將它們留白。自定義UITableViewCell通過TableView滾動時丟失內容

CellForRowAtIndexPath Method 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"Cell"; 
static NSString *GECIdentifier = @"GECell"; 
static NSString *TECIdentifier = @"TECell"; 
static NSString *AMCIdentifier = @"AMCell"; 

if (indexPath.section == 0) { 
    //Create generic GEC for use by all rows in section 
    GeneralEditingCell *GEC = [self.tableView dequeueReusableCellWithIdentifier:GECIdentifier]; 
    if (GEC == nil) { 
     GEC = [[GeneralEditingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     GEC.accessoryType = UITableViewCellAccessoryNone; 
     GEC.selectionStyle = UITableViewCellSelectionStyleNone; 
     GEC.master = self; 
    } 

    //Create and setup datepicker and dateformatter for later use 
    UIDatePicker *datePicker = [[UIDatePicker alloc]init]; 
    datePicker.datePickerMode = UIDatePickerModeDate; 
    datePicker.tag = indexPath.row; 
    [datePicker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged]; 

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
    [formatter setDateFormat:@"LLL dd, yyyy"]; 

    //Configure each cell individually 
    switch (indexPath.row) { 
     case 0: 
      if (editing) { 
       GEC.titleField.text = [item objectForKey:@"Title"]; 
       GEC.dateField.text = [item objectForKey:@"Date"]; 
       GEC.imageView.image = [self getThumbnailforVideoAtPath:[NSURL fileURLWithPath:[item objectForKey:@"Video Path"]]]; 
      } else { 
       GEC.dateField.text = [formatter stringFromDate:[NSDate date]]; 
       GEC.dateField.inputView = datePicker; 
      } 
      break; 
     default: 
      break; 
    } 
    return GEC; 
} 

if (indexPath.section == 1) { 
    //Create generic TEC for use by all rows in section 
    TextEditingCell *TEC = [self.tableView dequeueReusableCellWithIdentifier:TECIdentifier]; 
    if (TEC == nil) { 
     TEC = [[TextEditingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     TEC.accessoryType = UITableViewCellAccessoryNone; 
     TEC.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    //Configure each cell individually 
    switch (indexPath.section) { 
     case 0: 
      if (editing) { 
       TEC.textField.text = @"Editing Mode"; 
      } else { 
       TEC.label.text = @"TextEdit Cell"; 
      } 
      break; 
     default: 
      break; 
    } 
    return TEC; 
} 

if (indexPath.section == 2) { 
    //Create generic AMC for use by all rows in section 
    AddMediaCell *AMC = [self.tableView dequeueReusableCellWithIdentifier:AMCIdentifier]; 
    if (AMC == nil) { 
     AMC = [[AddMediaCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     AMC.accessoryType = UITableViewCellAccessoryNone; 
     AMC.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    //Configure each cell individually 
    switch (indexPath.row) { 
     case 0: 
      AMC.label.text = @"Add Video"; 
      break; 
     case 1: 
      AMC.label.text = @"Add Photos"; 
      break; 
     case 2: 
      AMC.label.text = @"Record Audio"; 
      break; 
     case 3: 
      AMC.label.text = @"Add Text Note"; 
      break; 
     case 4: 
      AMC.label.text = @"Attach Location"; 
      break; 
     case 5: 
      AMC.label.text = @"Attach Weather Data"; 
      break; 
     case 6: 
      AMC.label.text = @"Add More Tags"; 
      break; 
     default: 
      break; 
    } 
    return AMC; 
} 

//Catch any previously unhandled cell 
UITableViewCell *errorCell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (errorCell == nil) { 
    errorCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
errorCell.textLabel.text = @"ERROR! UNHANDLED CELL!!!"; 
return errorCell; 
} 
+0

當你分配單元格時,如果它們出現'nil',爲什麼不使用它們的類別標識符而不是通用的'CellIdentifier'? –

回答

0

您只顯示imageView的一種用法,但也許這種修改將修復該問題並且具有啓發性。這個想法是針對你的單元格子類中的任何屬性,你不能假定任何給定的屬性可能會顯示。因此,對於每一個將它設置或清除:

switch (indexPath.row) { 
    case 0: 
     if (editing) { 
      GEC.titleField.text = [item objectForKey:@"Title"]; 
      GEC.dateField.text = [item objectForKey:@"Date"]; 
      GEC.dateField.inputView = nil; 
      GEC.imageView.image = [self getThumbnailforVideoAtPath:[NSURL fileURLWithPath:[item objectForKey:@"Video Path"]]]; 
     } else { 
      GEC.titleField.text = @""; 
      GEC.dateField.text = [formatter stringFromDate:[NSDate date]]; 
      GEC.dateField.inputView = datePicker; 
      GEC.imageView.image = nil; 
     } 
     break; 
    default: 
     break; 
0

我有一個類似的問題(左右滾動時數據不保留),但我的是一個指數計數錯誤是有細胞顯示什麼。

什麼幫我調試它是:

  • 設置`的cellForRowAtIndexPath」休息
  • 禁用
  • 運行應用程序和向下滾動
  • 允許斷點再次
  • 回捲up

UITableView automatica在屏幕上顯示免費的內存UITableViewCell行,並在您向後滾動時再次調用cellForRowAtIndexPath

您可能錯過了一次只爲每行初始化UIImageView的情況,但當您再次調用它時會出現某些錯誤。

相關問題