Цель-C: Как сохранить входные данные, а также имена тегов, когда UItextFields прокручивается? - PullRequest
0 голосов
/ 14 февраля 2012

Мое приложение размещает вопросы и, в зависимости от вопроса, размещает UITextField или UISwitch.

Когда пользователь вводит тексты, он автоматически определяет, какой textField и размещает тексты соответственно.

Работает хорошо, но когда элементы прокручиваются, он также удаляет пользовательские вводы и имена тегов,и когда отображается область размещения новых элементов поверх этого.

Поэтому, когда пользователь вводит текст, он сохраняет его в старом textField.

Я хотел бы знать, как предотвратить егоэтот вопрос.

Есть ли какие-либо предложения?Заранее спасибо.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
NSLog(@"-------------cellForRowAtIndexPath---------------");   

cell_id = [qid objectAtIndex:[indexPath row] ];       
static NSString *CellIdentifier = @"Cell";    
label = nil; 
cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    [self configureLabel];        
    [[cell contentView] addSubview:label];
}

dict = [qtext objectAtIndex:[indexPath row]];
celltext = [NSString stringWithFormat:@"%@\n\n\n",[[dict allKeys] objectAtIndex:0]];
dict = [qtype objectAtIndex:[indexPath row]];
type = [[dict allKeys] objectAtIndex:0];     

 //place the question
 cell.textLabel.text = celltext;  

    NSLog(@"celltext=%@",celltext);
    if([type isEqualToString:@"devider"]){      
        [self configureDevider];
    }else{
        [self configureCell];      
    }
    if([cell_id intValue] == ([qid count])){
    tabledone = @"Yes";
    }

tableView.backgroundColor=[UIColor clearColor];
tableView.opaque=NO;
tableView.backgroundView=nil;

NSString *a = [arrAllheight objectAtIndex:[indexPath row]];


allheight +=thisheight; 
thisheight =[a intValue];   



if([type isEqualToString:@"YN"]){

    DCRoundSwitch *ynSwitch = [[DCRoundSwitch alloc] initWithFrame:CGRectMake(220,thisheight-40,80,27)] ;  

    ynSwitch.onText=@"Yes";
    ynSwitch.offText=@"No";
    [answers addObject:ynSwitch];
    [cell addSubview:ynSwitch];
    [ynSwitch setTag:[cell_id intValue]]; 
    [ynSwitch addTarget:self  action:@selector(setAnswersForRoundSwitches:) forControlEvents:UIControlEventValueChanged];

    i++;


}else if([type isEqualToString:@"freetext"]){

    //When the done button was clicked, remove the keybords from the screen
    [self makeTextField];

    [rtxtfield addTarget:self  action:@selector(setAnswersfortextFields:) forControlEvents:UIControlEventEditingDidEndOnExit];
    //    [rtxtfield value]; 



}else if([type isEqualToString:@"dropdown"]){

    picc = [picker_array objectForKey:[[NSString alloc]  initWithFormat:@"%d",cell_id]];

    //Choose an array for this textField
    // [self getPickerArray];
    [self makeTextField];
    //[rtxtfield addTarget:self  action:@selector(setAnswersfortextFields:) forControlEvents:UIControlEventEditingDidEndOnExit];  

    //When the done button was clicked, remove the keybords from the screen
    [rtxtfield addTarget:self action:@selector(textFieldReturn:) forControlEvents:UIControlEventEditingDidEndOnExit];
    //Get the tag for picker
    [rtxtfield addTarget:self action:@selector(getTag:) forControlEvents:UIControlEventTouchDown];     
    //Display picker
    [rtxtfield addTarget:self action:@selector(acsheet:) forControlEvents:UIControlEventTouchDown];     
    //set Tag for the textField
    [rtxtfield setTag:[cell_id intValue]];
       NSLog(@"rtxtfield tag=%d",rtxtfield.tag);

}


if([type isEqualToString:@"devider"]){   
[self caliculateHeightofCell];
}else{
[self caliculateHeightofCell];
}  


return cell;

}

Ответы [ 4 ]

3 голосов
/ 14 февраля 2012

Сохраните состояние ваших элементов управления в вашей модели данных , как только они изменятся .Итак, возможно, ваша модель представляет собой массив вопросов, и у каждого вопроса есть переменная экземпляра, которая может содержать ответ.Ваш контроллер представления, вероятно, является как источником данных таблицы, так и делегатом таблицы, и вы должны также сделать его целью любого элемента управления в ячейках.То есть, когда вы устанавливаете новую ячейку в -tableView:cellForRowAtIndexPath:, сделайте контроллер представления целью UITextField или UISwitch в ячейке.Когда пользователь изменяет любой из этих элементов управления, тогда изменение вызовет действие в контроллере представления, и контроллер представления может извлечь новое значение элемента управления и сохранить его в соответствующем вопросе в модели данных.

Если вы выберете такой подход, вам никогда не придется беспокоиться о том, какие вопросы не видны.Как только вопрос снова появится в поле зрения, для этой строки снова будет вызван -tableView:cellForRowAtIndexPath:, и у вас будет вся информация, необходимая для восстановления этой ячейки.

2 голосов
/ 14 февраля 2012

Сохранить текст в источник данных. UITableViewCells не должен содержать никакой информации о состоянии.

Реализуйте что-то похожее на это:

- (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];
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(2, 2, 200, 40)];
        textField.tag = 999;
        textField.delegate = self;
        textField.placeholder = @"Enter text here";
        [cell.contentView addSubview:textField];
    }
    UITextField *textField = (UITextField *)[cell.contentView viewWithTag:999];
    textField.text = [self.dataSource objectAtIndex:indexPath.row];
    /* configure cell */
    return cell;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    UIView *contentView = [textField superview];
    UITableViewCell *cell = (UITableViewCell *)[contentView superview];
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.dataSource replaceObjectAtIndex:indexPath.row withObject:textField.text];
}

Не добавляйте представления за пределами if (cell == nil)!
Если выдля разных типов ячеек используйте другой CellIdentifierКак это:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SwitchCellIdentifier = @"SwitchCell";
    static NSString *TextFieldCellIdentifier = @"TextFieldCell";

    UITableViewCell *cell = nil;
    if (/* current cell is a text field cell */) {
        cell = [tableView dequeueReusableCellWithIdentifier:TextFieldCellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextFieldCellIdentifier];
            // add textField
        }
        // configure cell...
    }
    else if (/* current cell is a switch cell */) {
        cell = [tableView dequeueReusableCellWithIdentifier:SwitchCellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SwitchCellIdentifier];
            // add switch
        }
        // configure cell...
    }
    return cell;
}
0 голосов
/ 15 февраля 2012

Я потратил один день, чтобы решить проблему, но, наконец, понял: правильно, это длинный код, потому что он использует textField, DCRoundSwitch и pickerView.

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
   NSLog(@"-----------------cellForRowAtIndexPath---------------");       

  Questions *q = [qtext objectAtIndex:indexPath.row];
  NSLog(@"question=%@",q.question);
   AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  static NSString *SwitchCellIdentifier = @"SwitchCell";
  static NSString *TextFieldCellIdentifier = @"TextFieldCell";   
  static NSString *DropDownFieldCellIdentifier = @"DropDownCell";  

  cell = nil;
  cell = [tableView dequeueReusableCellWithIdentifier:TextFieldCellIdentifier];
  if ([q.question_type isEqualToString:@"freetext"]) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextFieldCellIdentifier];

    if (cell == nil) {
    [self makeTextField];
        [cell.contentView addSubview:rtxtfield];            
        cell.textLabel.text = q.question;
        }

    [self makeTextField];
    [self configureCell];
    [self configureLabel];

    cell.textLabel.text = q.question;
    [cell.contentView addSubview:rtxtfield];


    rtxtfield.text = [appDelegate.dataSource objectAtIndex:indexPath.row]; 
    rtxtfield.tag=[indexPath row];



}else if([q.question_type isEqualToString:@"YN"]){  
      AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SwitchCellIdentifier];

    if (cell == nil) {
   [self makeDCRoundSwitch];
   [ynSwitch setTag:[indexPath row]]; 
   cell.textLabel.text = q.question;
    [self configureLabel];
    [self configureCell];
    }
    [self makeDCRoundSwitch];
    [ynSwitch setTag:[indexPath row]]; 
    cell.textLabel.text = q.question;
    [self configureLabel];     
    [self configureCell];

    [ynSwitch addTarget:self  action:@selector(ynSwitchDidEndEditing:) forControlEvents:UIControlEventValueChanged];
    if([[appDelegate.dataSource objectAtIndex:indexPath.row] isEqualToString:@"Yes"]){
        [ynSwitch setOn:YES animated:YES];
    }  else{
        [ynSwitch setOn:NO animated:YES]; 
    }

    rtxtfield.tag=[indexPath row];    


   } else if([q.question_type isEqualToString:@"dropdown"]){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DropDownFieldCellIdentifier];

    if (cell == nil) {
        [self makeTextField];
        [cell.contentView addSubview:rtxtfield];            
        cell.textLabel.text = q.question;
    }

    [self makeTextField];
    [self configureCell];
    [self configureLabel];

    cell.textLabel.text = q.question;
    [cell.contentView addSubview:rtxtfield];
    picc=q.question_array;

    //When the done button was clicked, remove the keybords from the screen
    [rtxtfield addTarget:self action:@selector(textFieldReturn:) forControlEvents:UIControlEventEditingDidEndOnExit];
    //Get the tag for picker
    [rtxtfield addTarget:self action:@selector(getTag:) forControlEvents:UIControlEventTouchDown];     
    //Display picker
    [rtxtfield addTarget:self action:@selector(acsheet:) forControlEvents:UIControlEventTouchDown];     
    //set Tag for the textField
    [rtxtfield setTag:[indexPath row]];      



   //  AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    rtxtfield.text = [appDelegate.dataSource objectAtIndex:indexPath.row]; 
    rtxtfield.tag=[indexPath row];

}


 [self makeTextField];
 rtxtfield.tag=[indexPath row];

tableView.backgroundColor=[UIColor clearColor];
tableView.opaque=NO;
tableView.backgroundView=nil;


return cell;

 }
0 голосов
/ 14 февраля 2012

попробуйте

метка = ноль;

//cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    [self configureLabel];        
    [[cell contentView] addSubview:label];
//}

прокомментируй свой код и скажи мне, что происходит

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...