根据匹配来自 coredata 的字符串选择 UIPickerview
Posted
技术标签:
【中文标题】根据匹配来自 coredata 的字符串选择 UIPickerview【英文标题】:Select UIPickerview based on matching a string from coredata 【发布时间】:2014-02-20 21:21:07 【问题描述】:我有一个表单,一旦用户提交,它应该重新加载视图(但要更改一些内容)。
在这个表单上有一个 UIPickerview (_tripPicker),它有 2 个位置,一个起始位置和一个结束位置(pickerview 的 2 个组件)。
我将它保存到适当的数据库以及所有这些 - 但是当它重新加载时(当用户单击保存时)我希望选择器视图“重置”和第一个位置(begSchool),以匹配用户的第二个位置( endSchool) 他们刚刚保存到 coredata。
例如:假设用户从点 B 到点 C(分别在选择器视图中的组件 0 和 1);当他们单击保存时,我希望组件 0 显示“PointC”,而第二个组件仅显示要转到的点列表(将其重置为最初的加载方式)。
我试图尝试做一些匹配的字符串,但我遇到了问题。
这是我这样做的逻辑:
//Save the data & reload View
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error)
//Reset the pickerview **********THIS LOGIC NEEDS WORK**********
//Check to see if there is previous UserMiles entered - if so, set beg_school to appropriate name
// Get the local context
NSArray *tripsSorted = [UserMiles MR_findAllSortedBy:@"driven_date" ascending:NO];
if (!tripsSorted || !tripsSorted.count)
//if no previous trips have been entered - set the school list to default
begSchoolLabel.text = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]];
else
UserMiles *lastTrip = [tripsSorted objectAtIndex:0];
NSString *preValue = lastTrip.end_school;
begSchoolLabel.text = preValue;
NSUInteger *currentIndex = [_schoolArray1 indexOfObject:preValue]; //Error/warning that incompatible integer to point conversion
[_tripPicker selectRow:currentIndex inComponent:0 animated:YES]; //Error/warning here that incompatible point to integer conversion
NSLog(@"LastTrip.endSchool = %@", lastTrip.end_school);
//Set end school labels for the second component in the picker
endSchoolLabel.text = [_schoolArray2 objectAtIndex:[_tripPicker selectedRowInComponent:1]];
NSLog (@"saveInBackground: finished!");
];
[self.view setNeedsDisplay];
lastTrip.end_school 从 pickerview 的组件 1 中获取学校的适当名称,我只需要弄清楚如何将其与正在加载 pickerview 的数组中的适当值匹配并使其在 pickerview 中被选中.文本当前显示适当的名称,但选择器视图没有显示任何更改。
如果我需要澄清或您需要查看哪些其他代码,请告诉我 - 提前致谢。
【问题讨论】:
【参考方案1】:我能够通过首先通过将字符串与数组中的对象进行匹配来找到数组中的 indexValue 来解决此问题。
我最终得到了这个:
//Save the data & reload View
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error)
//Reset the pickerview **********THIS LOGIC NEEDS WORK**********
//Check to see if there is previous UserMiles entered - if so, set beg_school to appropriate name
// Get the local context
NSArray *tripsSorted = [UserMiles MR_findAllSortedBy:@"driven_date" ascending:NO];
if (!tripsSorted || !tripsSorted.count)
//if no previous trips have been entered - set the school list to default
begSchoolLabel.text = [_schoolArray1 objectAtIndex:[_tripPicker selectedRowInComponent:0]];
else
UserMiles *lastTrip = [tripsSorted lastObject];
NSString *preValue = lastTrip.end_school;
begSchoolLabel.text = preValue;
int indexValue = [_schoolArray1 indexOfObject:preValue]; //Compares the preValue string to the strings in the array and finds the indexValue of the right match
[_tripPicker selectRow:indexValue inComponent:0 animated:YES]; //Sets the picker to the appropriate value
NSLog(@"LastTrip.endSchool = %@", lastTrip.end_school);
//Set end school labels for the second component in the picker
endSchoolLabel.text = [_schoolArray2 objectAtIndex:[_tripPicker selectedRowInComponent:1]];
NSLog (@"saveInBackground: finished!");
];
[self.view setNeedsDisplay];
【讨论】:
以上是关于根据匹配来自 coredata 的字符串选择 UIPickerview的主要内容,如果未能解决你的问题,请参考以下文章
来自先前选择器的选择器值 - CoreData/SwiftUI
在 jQuery UI datepicker 中仅选择特定日期(日期列表来自 AJAX)