2010-08-19 53 views
1

我有一個異構tableView與整個部分組成的UITableViewCells與UITextField控件在每個人的contetView。問題在於,當我滾動時,我看到文本字段中的文本被混合在一起。UITableView與自定義UITableViewCells混合滾動內容

在這個問題出現之前,我已經見過很多次了,雖然已經廢棄了,但我通過爲每種單元格分配了不同的cellIdentifier來解決這個問題。

現在有什麼不同呢是我動態地創建單元格,並且根據服務器的答案,表格部分可能有零個或多個自定義單元格(具有textField的單元格)。現在

,這很有趣地看到,textfiled的屬性,如佔位符是整個卷軸執着,但我寫的文字不具有持續性可言,文字改變它的位置...

我有一個視頻here所以你可以看到我的意思。

我使用的代碼是這樣的:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 
    NSString *varCellID = [NSString stringWithFormat:@"%i_VarCellID", (indexPath.row + 7891)]; 

    if (indexPath.section == 0) { 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      [self configureCellViews:cell indexPath:indexPath]; 
     } 
     [self configureCellContent:cell indexPath:indexPath]; 
     return cell; 
    } 
    else if (indexPath.section == 1) { 
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
      [self configureCellViews:cell indexPath:indexPath]; 
     } 
     [self configureCellContent:cell indexPath:indexPath]; 
     return cell; 
    } 
    else { 
      UITableViewCell *varCell = [tableView dequeueReusableCellWithIdentifier:varCellID]; 

     if (varCell == nil) { 
      varCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:varCellID] autorelease]; 
      varCell.selectionStyle = UITableViewCellSelectionStyleNone; 
      [self configureCellViews:varCell indexPath:indexPath]; 
     } 
     [self configureCellContent:varCell indexPath:indexPath]; 
     return varCell; 
    } 
} 

我知道這不是優雅,但woked之前。除了dinamically創建的單元格標識符是我(絕望)嘗試在每個textField上保留單獨內容之一。

下面是如何創建單元格的觀點和內容:

#pragma mark - 
#pragma mark ConfigureCellcontent 

- (void)configureCellViews:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath { 

    NSArray *aSection = [sections objectAtIndex:indexPath.section]; 
    NSDictionary *aField; 
    int varformatid; 

    if (indexPath.section == 0) { 

     cell.detailTextLabel.textAlignment = UITextAlignmentLeft; 
    } 
    else if (indexPath.section == 1) { 

     cell.detailTextLabel.textAlignment = UITextAlignmentLeft; 
    } 
    else { 

     aField = [aSection objectAtIndex:indexPath.row]; 
     varformatid = [[aField valueForKey:@"varformatid"] intValue]; 
     NSArray *aSection = [sections objectAtIndex:indexPath.section]; 
     NSLog(@"format id %@", [[aSection objectAtIndex:0] valueForKey:@"varname"]); 

     fieldLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 7, 100, 30)]; 
     fieldLabel.tag = 1234; 
     fieldLabel.textAlignment = UITextAlignmentLeft; 
     fieldLabel.font = [UIFont boldSystemFontOfSize:14]; 
     fieldLabel.adjustsFontSizeToFitWidth = YES; 
     [cell.contentView addSubview:fieldLabel]; 

     theTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 12, 170, 30)]; 
     theTextField.textColor = kAnswersTextColor; 
     theTextField.returnKeyType = UIReturnKeyDone; 
     theTextField.tag = indexPath.row + 101; 
     theTextField.adjustsFontSizeToFitWidth = YES; 
     theTextField.delegate = self; 


     if(varformatid == 6 || varformatid == 7 || varformatid == 8) { 
      // use number pad input 
      theTextField.keyboardType = UIKeyboardTypeNumberPad; 
      [cell addSubview: theTextField]; 
      [theTextField release]; 
     } 
     else if(varformatid == 4) { 
      // use date 
     } 
     else if(varformatid == 17 || varformatid == 18) { 
      // use pull down, elements of pull down are in XML 
     } 
     else if(varformatid == 20) { 
      theSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(120, 8, 100, 30)]; 
      theSwitch.selected = YES; 
      [cell.contentView addSubview:theSwitch]; 
     } 
     else { 
      // use input string 
      [cell addSubview: theTextField]; 
      [theTextField release]; 
     }  
    } 

} 

- (void)configureCellContent:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath { 

    NSArray *aSection = [sections objectAtIndex:indexPath.section]; 
    NSString *aField; 


    if (indexPath.section == 0) { 
     aField = [[aSection objectAtIndex:0] valueForKey:@"SetDate"]; 
     cell.textLabel.text = @"Set Date"; 
     cell.detailTextLabel.text = aField; 
    } 
    else if (indexPath.section == 1) { 
     aField = [[aSection objectAtIndex:0] valueForKey:@"SetType"]; 
     cell.textLabel.text = @"Set Type"; 
     cell.detailTextLabel.text = aField; 
    } 

    else { 

     aField = [[aSection objectAtIndex:indexPath.row] valueForKey:@"varname"]; 
     if ([[[aSection objectAtIndex:indexPath.row] valueForKey:@"varrequired"] isEqualToString:@"yes"]) { 
      [(UITextField *)[cell viewWithTag:indexPath.row + 101] setPlaceholder: @"Required"]; 
     } 
     else [(UITextField *)[cell viewWithTag:indexPath.row + 101] setPlaceholder: @"Optional"]; 
     [(UILabel *)[cell.contentView viewWithTag:1234] setText: aField]; 

    } 
} 

最後,我請求你的幫助,以找到至少一個更好的辦法。提前致謝。

回答

2

你的configureCellContent似乎壞了。所以你要麼沒有用鍵入的文本更新你的內部數據結構(你可能希望在每次文本更改時都這樣做),否則你會錯過在configureCellViews中填寫正確的值。

旁註:您不應該在頂部出列兩個單元格。只要出隊正確的一個!即把它放到如果部分。

+0

謝謝,這幫了我,現在我對輸入的文本有了更多的控制。每次爲每個textField調用textFieldDidEndEditing:方法時,現在我都有相同數量的節和行的數組來存儲文本。儘管如此,我仍然可以看到內容混雜,只有在第一行和最後一行之間纔會發生。 – chuckSaldana 2010-08-19 19:49:02

2

我以前遇到過這個。我相信這是因爲UITableViewCells在不可見時被釋放以減少內存消耗。當您向後滾動時,您會爲該單元格獲取新出隊的實例。

您需要將單元格的狀態保存到某處,以便在返回到視圖中時重新初始化該單元格。當你保存完全取決於你 - 當你的UITextField失去焦點時,或者當字段以任何方式改變時。

+0

好點,實際上我通過創建一個鏡像數組來解決問題,該數組將包含文本字段中的所有字符串,並且每次創建新單元格時,該方法都會檢查相應數組索引中是否有內容將其放入文本字​​段中。 。 – chuckSaldana 2010-10-19 15:20:03

1

我不知道這是否是您要參加的解決方案,但您可以修改CellIdentifier並使其唯一。

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row]; 
0

的NSString * CellIdentifier = [NSString的stringWithFormat:@ 「格%d」,indexPath.row]; 代替靜態NSString * CellIdentifier = @「Cell」;

是在快速滾動的情況下混合行的解決方案。

相關問題