【问题标题】:How to take 2 input value from a UITableViewCell in objective c如何从目标 c 中的 UITableViewCell 获取 2 个输入值
【发布时间】: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


【解决方案1】:

以字典为全局变量。在字典中添加每个值,然后在将字典添加到数组时检查字典的计数是否为 3,然后将字典添加到数组中,添加后使字典为空。

【讨论】:

    猜你喜欢
    • 2018-09-17
    • 2017-04-02
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 2016-10-30
    • 2022-01-12
    相关资源
    最近更新 更多