【发布时间】:2017-11-19 07:11:17
【问题描述】:
我需要从同一个 UITableViewCell 中获取 2 个输入值,例如文本字段 1 和文本字段 2 [得分 1,得分 2],并将这些值添加到字典中,并将这些字典数据添加到 NSMutableArray。
我用文本字段委托方法尝试了下面的代码..
- (void)textFieldDidEndEditing:(UITextField *)textField{
UITableViewCell *cell = (UITableViewCell*) textField.superview.superview;
NSIndexPath *txtIndPath = [tblMonthlyPerformanceReport indexPathForCell:cell];
MonthlyPerformanceQuestions * question;
if (txtIndPath.section==0)
{
question = [[HSEAppData appData].arrayMonthlyGeneralQustion objectAtIndex:txtIndPath.row];
}
if (txtIndPath.section==1)
{
question = [[HSEAppData appData].arrayMonthlyLeadQustion objectAtIndex:txtIndPath.row];
} if (txtIndPath.section==2)
{
question = [[HSEAppData appData].arrayMonthlyLagQustion objectAtIndex:txtIndPath.row];
}
NSLog(@"%@ , %@",question.QuestionId, question.Question);
NSMutableDictionary *userDictionary=[[NSMutableDictionary alloc]init];
[userDictionary setObject:question.QuestionId forKey:@"question"];
if (textField.tag==10001)
{
[userDictionary setObject:textField.text forKey:@"score1"];
}
if (textField.tag==10002)
{
[userDictionary setObject:textField.text forKey:@"score2"];
}
[scoreMutableArray addObject:userDictionary];
我收到了回复
{
question = a5k0k0000004kxsAAA;
score2 = 5;
}
我将数据放在分数 1 上,但数据放在分数 2 上 我需要这样的字典数据
{
"question": "a5k0k0000004kxnAAA",
"score1": "20",
"score2": "20"
}
请帮帮我
【问题讨论】:
-
在将替换字符串添加到预先存在的文本后,您应该使用
shouldChangeCharacters,而不是使用textFieldDidEndEditing。如果用户按下某些操作直接进入下一个屏幕而不关闭键盘,有时会出现错误。使用模型而不是直接更改字典键(更好的做法),因为字符串值可能输入错误或更改,只需编写一个解析器直接将其转换为NSDictionary
标签: ios uitextfield uikit