iPhone SDK: Сохранение текста UITextField в пользовательском UITableViewCell с помощью свитков? - PullRequest
0 голосов
/ 09 июля 2010

Мне известно, что для сохранения памяти UITableViewCells повторно используется при прокрутке. У меня есть несколько пользовательских UITableViewCells, в которых есть UITextFields. После ввода текста в UITextField и прокрутки этот текст теряется из-за повторного использования ячейки.

Как сделать так, чтобы этот текст сохранялся в свитках? Я думаю, что могу хранить текст каждой ячейки в своем собственном индексе NSMutableArray. Затем, когда ячейка используется повторно, я могу заполнить ее данными массива.

Как бы я поступил так? Будет ли кто-то так любезно опубликовать пример кода?

1 Ответ

1 голос
/ 09 июля 2010

sample.h

@interface sample : UIViewController <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{

}

@end

sample.m

-(void)viewWillAppear:(BOOL)animated
{

    arrData = [[NSMutableArray alloc]init];
    for (int i=0; i<10; i++) // 10- number of fields
    [arrData addObject:@""];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
   return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return [arrData count];

}


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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
     UITextField *txt = [[UITextField alloc]initWithFrame:frm];
     txt.autocorrectionType = UITextAutocorrectionTypeNo;
     txt.tag = indexPath.row;
     txt.delegate = self;
     txt.text = [arrData objectAtIndex:indexPath.row];

     [cell addSubview:txt];
     [txt release];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
   [arrData replaceObjectAtIndex:[textField tag] withObject:textField.text];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...