2015-03-02 41 views
0

我正在實現一個帶有文本視圖的UITableView作爲我的單元格的內容視圖。當用戶返回到屏幕UITextViews在UITableView中被選中時未被清除

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    if (textField == _textFieldOne){ 
     [settingsDictionary setObject: _textFieldOne.text forKey:@"EntryOneText"]; 
     [settingsDictionary stringValueForKey:@"textFieldOneValue"]; 
     [self postNotificationSettingsUpdate:settingsDictionary]; 
     didTestPostNotificationSettings = YES; 
} 

這些保存的值應顯示在文本字段:當用戶點擊返回鍵

用戶輸入的數據被保存在設置字典,我已經用下面的代碼完成:

//Set the value in the text fields from the settings dictionary 
    NSString *textFieldOneText = [self.settingsDictionary  stringValueForKey:@"RedZoneCarsPerManHrMin"]; 

    _textFieldOne.text = textFieldOneValue; 

到目前爲止,一切似乎都按預期方式工作:文本輸入被保存,並在文本字段中顯示,當用戶返回到屏幕。然而,如果TableView中的保持所述文本字段是選擇(不文本字段本身),文本字段顯示以下所示的行爲:

enter image description here

似乎就好像文本字段顯示新輸入的條目以及上次保存的條目一樣。

編輯 I'ved增加了更多的下面我的cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubscribedRedZoneAlertRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"RedZoneAlertCell"; 

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

    cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1]; 

    UISwitch *cellSwitch = nil; 

    NSNumber *position = enabledRedZoneAlertRows[indexPath.row]; 


switch (position.integerValue) { 
    case CarPerManHourRow: { 
     cell.textLabel.text = NSLocalizedString(@"Label", @"Label Row"); 
     cellSwitch = [[UISwitch alloc] init]; 
     cellSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:SwitchKey]; 
     cellSwitch.tag = SwitchTag; 
     [cellSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
     cell.accessoryView = cellSwitch; 

     if ([[self.settingsDictionary valueForKey:@"RedZoneCarsPerManHrOnOff"]isEqual: @"1"]){ 
      [cellSwitch setOn:YES]; 
     } 
     break; 
    } 
    case TextFieldRow: { 
     static NSString *CellIdentifier = @"ConfigCell"; 
     cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
     [cell.contentView addSubview:_textFieldOne]; 
     [cell.contentView addSubview:_textFieldTwo]; 
     [cell.contentView addSubview:_spacingLabel]; 
     } 

     cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1]; 

     _textFieldOne = [[UITextField alloc]initWithFrame:CGRectMake(25, 0, 50, 30)]; 
     _textFieldTwo = [[UITextField alloc]initWithFrame:CGRectMake(100, 0, 50, 30)]; 

     _textFieldOne.delegate = self; 
     _textFieldTwo.delegate = self; 

     _textFieldOne.placeholder = @"Auto"; 
     _textFieldTwo.placeholder = @"Auto"; 
     [_textFieldOne setTextAlignment:NSTextAlignmentCenter]; 
     [_textFieldTwo setTextAlignment:NSTextAlignmentCenter]; 

     //Set the value in the text fields from the settings dictionary 
     NSString *textFieldOneText = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMin"]; 
     NSString *textFieldTwoText = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMax"]; 

     NSString *carsPerManHrMax = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMax"]; 
     NSString *carsPerManHrMax = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMax"]; 

     _textFieldOne.text = textFieldOneValue; 
     _textFieldTwo.text = textFieldTwoValue 

     _textFieldOne.backgroundColor = [UIColor whiteColor]; 
     _textFieldTwo.backgroundColor = [UIColor whiteColor]; 
     _textFieldOne.tintColor = [UIColor blackColor]; 
     _textFieldTwo.tintColor = [UIColor blackColor]; 

     [_textFieldOne.layer setCornerRadius:7.0f]; 
     [_textFieldTwo.layer setCornerRadius:7.0f]; 

     [_textFieldOne setKeyboardType:UIKeyboardTypeNumbersAndPunctuation]; 
     [_textFieldTwo setKeyboardType:UIKeyboardTypeNumbersAndPunctuation]; 

     _textFieldOne.returnKeyType = UIReturnKeyNext; 
     _textFieldTwo.returnKeyType = UIReturnKeyDone; 

     _spacingLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 35, 30)]; 
     _spacingLabel.text = @"–"; 
     _spacingLabel.textColor = [UIColor colorWithRed:0.658 green:0.658 blue:0.658 alpha:1]; 
     [_spacingLabel setTextAlignment:NSTextAlignmentCenter]; 
     _spacingLabel.backgroundColor = [UIColor clearColor]; 

     //[cell.contentView addSubview:_textFieldOne]; 
     //[cell.contentView addSubview:_textFieldTwo]; 
     //[cell.contentView addSubview:_spacingLabel]; 

     break; 
    } 
    return cell; 
} 

編輯兩個 下面是我在基於jherran的答案固定我的問題的嘗試。不過,我仍然遇到同樣的問題。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubscribedRedZoneAlertRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"RedZoneAlertCell"; 

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

    cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1]; 

    UISwitch *cellSwitch = nil; 

    NSNumber *position = enabledRedZoneAlertRows[indexPath.row]; 

    switch (position.integerValue) { 
     case CarPerManHourRow: { 
      cell.textLabel.text = NSLocalizedString(@"Label", @"Label Row"); 
      cellSwitch = [[UISwitch alloc] init]; 
      cellSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:SWSettingCarsPerManHour]; 
      cellSwitch.tag = SaveCarsPerManHourTag; 
      [cellSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
      cell.accessoryView = cellSwitch; 

      if ([[self.settingsDictionary valueForKey:@"RedZoneCarsPerManHrOnOff"]isEqual: @"1"]){ 
       [cellSwitch setOn:YES]; 
      } 
      break; 
     } 
     case CarsPerManHourConfigRow: { 

      static NSString *CellIdentifier = @"ConfigCell"; 
      cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 

       cell.backgroundColor = [UIColor colorWithRed:0.922 green:0.937 blue:0.949 alpha:1]; 

       _carsPerManHourMinTextField = [[UITextField alloc]initWithFrame:CGRectMake(25, 0, 50, 30)]; 
       _carsPerManHourMaxTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 0, 50, 30)]; 

       _textFieldOne.delegate = self; 
       _textFieldTwo.delegate = self; 

       _textFieldOne.placeholder = @"Auto"; 
       _textFieldTwo.placeholder = @"Auto"; 
       [_textFieldOne setTextAlignment:NSTextAlignmentCenter]; 
       [_textFieldTwo setTextAlignment:NSTextAlignmentCenter]; 




       _textFieldOne.backgroundColor = [UIColor whiteColor]; 
       _textFieldTwo.backgroundColor = [UIColor whiteColor]; 
       _textFieldOne.tintColor = [UIColor blackColor]; 
       _textFieldTwo.tintColor = [UIColor blackColor]; 


       [_textFieldOne.layer setCornerRadius:7.0f]; 
       [_textFieldTwo.layer setCornerRadius:7.0f]; 

       [_textFieldOne setKeyboardType:UIKeyboardTypeNumbersAndPunctuation]; 
       [_carsPerManHourMaxTextField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation]; 

       _textFieldOne.returnKeyType = UIReturnKeyNext; 
       _textFieldTwo.returnKeyType = UIReturnKeyDone; 

       _spacingLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 35, 30)]; 
       _spacingLabel.text = @"–"; 
       _spacingLabel.textColor = [UIColor colorWithRed:0.658 green:0.658 blue:0.658 alpha:1]; 
       [_spacingLabel setTextAlignment:NSTextAlignmentCenter]; 
       _spacingLabel.backgroundColor = [UIColor clearColor]; 

       NSLog(@"%@", _textFieldOne.text); 
       NSLog(@"%@",_carsPerManHourMaxTextField.text); 

       [cell.contentView addSubview: _textFieldOne]; 
       [cell.contentView addSubview: _textFieldTwo]; 
       [cell.contentView addSubview:_spacingLabel]; 

       cell.tag = 1; 
      } 

      else { 
       _textFieldOne = (UITextField *)[cell.contentView viewWithTag:1]; 
       _textFieldTwo = (UITextField *)[cell.contentView viewWithTag:1]; 
       _spacingLabel = (UILabel *)[cell.contentView viewWithTag:1]; 
      } 

      NSString * _textFieldOne = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMin"]; 
      NSString *carsPerManHrMax = [self.settingsDictionary stringValueForKey:@"RedZoneCarsPerManHrMax"]; 

      _textFieldOne.text = carsPerManHrMin; 
      _textFieldTwo.text = carsPerManHrMax; 

如何修改我的代碼以避免此行爲發生?

+0

請向我們展示整個cellForRowAtIndexPathMethod。 – 2015-03-02 17:41:07

+0

嗨@EarlGrey,我在我的編輯中添加了大部分cellForRowAtIndexPathMethod。我無法添加整個方法,因爲它很長(它們是表格視圖特定部分中的單元格)。 – narner 2015-03-02 18:16:41

回答

1

檢查您的cellForRowAtIndexPath,因爲單元格被重用,您可能未加載單元格屬性。

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

    /* 
    * This is an important bit, it asks the table view if it has any available cells 
    * already created which it is not using (if they are offscreen), so that it can 
    * reuse them (saving the time of alloc/init/load from xib a new cell). 
    * The identifier is there to differentiate between different types of cells 
    * (you can display different types of cells in the same table view) 
    */ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

    /* 
    * If the cell is nil it means no cell was available for reuse and that we should 
    * create a new one. 
    */ 
    UILabel *label; 
    if (cell == nil) { 

     /* 
     * Actually create a new cell (with an identifier so that it can be dequeued). 
     */ 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease]; 

     label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; 
     cell.tag = 1; 
     [cell.contentView addSubview:label]; 

    } else { 

     label = (UILabel *)[cell.contentView viewWithTag:1]; 

    } 

    /* 
    * Now that we have a cell we can configure it to display the data corresponding to 
    * this row/section 
    */ 

    label.text = @"whatever"; 
} 

編輯:當你的電池出口和重複使用,您要添加視圖([cell.contentView addSubview:_textFieldOne])每個細胞的再生時。

+0

啊,我很高興看到你的編輯,這很有道理!換句話說,我只需要將我的textField添加到單元格的子視圖中一次,對嗎?我將如何實現? – narner 2015-03-02 19:25:23

+0

您必須在'cell == nil'時添加它。你在那裏初始化單元格,並且它是子視圖。就是這個方法。 – jherran 2015-03-02 19:30:13

+0

在我的答案中看到我全面的解決方案。 – 2015-03-02 19:31:20

0

好的,這是我懷疑的。每次將單元格從重用隊列中取出時,都會創建一個新的textfield實例並將其添加到內容視圖中。所以基本上內容視圖堆積着新的文本框。 你應該做的是

1)有一個自定義單元格,使細胞負責其內部

2)只暴露的配置方法傳遞的唯一類原始文本細胞

3)當thr cell被重用時調用該配置方法。 4)保留對自定義單元格內文本字段的引用,以便隨時更新文本值。當它在cellforrowatindexpath中回收時

+0

這使得什麼是錯的。恐怕我並不完全瞭解你的解決方案;你會介意多解釋一下嗎?我很抱歉! – narner 2015-03-02 19:35:50

+0

什麼具體應該更詳細? – 2015-03-02 20:46:17

相關問題