因此,只要輸入不是「NULL」,我就有一個UIPicker視圖,它從NSMutableArray中填充。無法從基於選擇的UIPickerView中刪除項目
所以我的選擇器顯示除NULL以外的所有值。
現在,我有一個UITextField框和一個按鈕。所以,無論我在文本字段中輸入什麼內容,並且單擊該按鈕,如果它與NSMutableArray(用於填充UIPickerView)中的任何內容相匹配,它將其設置爲NULL並刷新UIPicker,使其不會再次顯示。
出於某種原因,我可以將值設置爲NULL(使用NSLog進行檢查),但選擇器永遠不會更新,NSMutable Array也不會。
-(void) loadthepicker
{
NSMutableArray *getarray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"FilerNamesArray"]];
pickerLoaderArray=[[NSMutableArray alloc] init];
for (int j=0; j<20; j++) {
if ([[getarray objectAtIndex:j] isEqualToString:@"NULL"])
{
// do nothing..don't load
}
else // add that filter to pickerLoaderArray
{
[pickerLoaderArray addObject:[getarray objectAtIndex:j]];
}
} // end of for
[pickerView reloadAllComponents];
[pickerView selectRow:0 inComponent:0 animated:NO];
}
-(NSInteger)numberOfComponentsInPickerView:(NSInteger)component
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component
{
return [pickerLoaderArray count];
}
-(NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [pickerLoaderArray objectAtIndex:row];
}
按鈕:
- (IBAction)deleteButton:(id)sender {
NSUserDefaults *CheckFiltersUsed = [NSUserDefaults standardUserDefaults];
NSInteger myInt = [CheckFiltersUsed integerForKey:@"FiltersUsed"];
if (myInt<=20 && myInt>0) {
NSLog(@"number of filters used before deleting %ld",(long)myInt);
[CheckFiltersUsed setInteger:myInt-1 forKey:@"FiltersUsed"];
[CheckFiltersUsed synchronize];
// get names array
NSMutableArray *getarray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"FilerNamesArray"]];
NSArray *get=getarray;
// at location where name matches with selectedfilter..put NULL
for (int j=0; j<20; j++) {
if ( [[getarray objectAtIndex:j] isEqualToString:_filterToDelete.text] && isFilterDeleted==NO)
{
NSLog(@"------currently %d is %@",j,[getarray objectAtIndex:j]);
[getarray insertObject:@"NULL" atIndex:j];
NSLog(@"------now %d is %@",j,[getarray objectAtIndex:j]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"" message: @"Deleted" delegate: nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show];
isFilterDeleted=YES;
[[NSUserDefaults standardUserDefaults] setObject:getarray forKey:@"FilerNamesArray"];
[[NSUserDefaults standardUserDefaults]synchronize];
[self loadthepicker];
}
else
{
NSLog(@"No matching filter name");
}
} // end of for
//now save this array back.
}
else
{
NSUserDefaults *CheckFiltersUsed = [NSUserDefaults standardUserDefaults];
NSInteger myInt = [CheckFiltersUsed integerForKey:@"FiltersUsed"];
NSLog(@"Wrong number of filters!!!... %d",myInt);
}
}
你必須有_something_爲每個組件的每一行。 – matt
只有一列,所以我想這只是一個組件。我不確定每一行的含義是什麼。 – rbk