我已经阅读了很多关于我的问题,但我仍然无法指出确切的问题。 我已经尝试了一些如果这里的代码在堆栈但我不断收到错误。 下面的代码有效,但在滚动时消失了文本。
有什么想法吗?
GetQuestionsCustomCell.h:
@interface GetQuestionsCustomCell : UITableViewCell <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UILabel *labelQuestion;
@property (strong, nonatomic) IBOutlet UITextField *textFieldAnswer;
@property (strong, nonatomic) IBOutlet UILabel *labelQuestionNumber;
- (IBAction)KeyDown:(id)sender;
@end
GetQuestionsCustomCell.m:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"GetQuestionsCustomCell" owner:self options:nil];
self = [nibArray objectAtIndex:0];
self.textFieldAnswer.delegate = self;
}
return self;
}
SecurityQuestions.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
GetQuestionsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GetQuestionsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSInteger nextIndexPathRow = indexPath.row;
nextIndexPathRow++;
cell.labelQuestion.text = [arrayOfQuestions objectAtIndex:indexPath.row];
cell.labelQuestionNumber.text = [NSString stringWithFormat:@".%d", nextIndexPathRow];
switch (indexPath.row) {
case 0:
tfA1 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 1:
tfA2 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 2:
tfA3 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 3:
tfA4 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 4:
tfA5 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 5:
tfA6 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 6:
tfA7 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 7:
tfA8 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 8:
tfA9 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 9:
tfA10 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 10:
tfA11 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 11:
tfA12 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
}
return cell;
}
答案 0 :(得分:7)
确切的问题是你既没有保存文本字段的值,也没有在cellForRowAtIndexPath
上设置它们
当用户在文本字段中输入内容时,您需要将值追踪到某处,也可以在ViewController中以问号编号索引的数组中。一种方法是跟踪cellForRowAtIndexPath
中的单元格:
cell.textFieldAnswer.tag = indexPath.row;
cell.textFieldAnswer.delegate = self;
然后你需要在viewcontroller中实现- (void)textFieldDidEndEditing:(UITextField *)textField
。将值保存为:
[self.answers replaceObjectAtIndex:textField.tag withObject: textField.text];
然后,您需要在cellForRowAtIndexPath
中再次将值设置为[cell.textFieldAnswer setText:[self.answers objectAtIndex:indexPath.row]]
原因是,如果您有1000个问题,iOS将不会创建1000个单元格。它将重用细胞。您需要更多地了解tableView:TableView Programming Guide
希望这有帮助。
答案 1 :(得分:1)
更改CellForRowatIndexPath ...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
GetQuestionsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GetQuestionsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
switch (indexPath.row) {
case 0:
tfA1 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 1:
tfA2 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 2:
tfA3 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 3:
tfA4 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 4:
tfA5 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 5:
tfA6 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 6:
tfA7 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 7:
tfA8 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 8:
tfA9 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 9:
tfA10 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 10:
tfA11 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
case 11:
tfA12 = (UITextField*)[cell.contentView viewWithTag:nextIndexPathRow];
break;
}
NSInteger nextIndexPathRow = indexPath.row;
nextIndexPathRow++;
cell.labelQuestion.text = [arrayOfQuestions objectAtIndex:indexPath.row];
cell.labelQuestionNumber.text = [NSString stringWithFormat:@".%d", nextIndexPathRow];
[tfA1 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA2 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA3 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA4 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA5 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA6 setText:[self.answers objectAtIndex:indexPath.row]];
[tfA7 setText:[self.answers objectAtIndex:indexPath.row]];
}
return cell;
}
答案 2 :(得分:0)
这是一个使用 removeFlashCard(pos: number) {
this.flashCards.removeAt(pos);
}
的简单解决方法,您可以在此方法中保存tableView delegate method
的值,
UITextview.text