2014-04-18 94 views
0

因此,只要輸入不是「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); 

    } 

} 
+0

你必須有_something_爲每個組件的每一行。 – matt

+0

只有一列,所以我想這只是一個組件。我不確定每一行的含義是什麼。 – rbk

回答

0

如果我得到你正在嘗試做的,你想從陣列中刪除和選擇器平等的字符串爲好。但不是你剛剛插入其它的NSString對象爲索引 'J'

在deleteButton方法:

代替此行的

[getarray insertObject:@"NULL" atIndex:j]; 

呼叫

[getarray removeObjectAtIndex:j]; 

**更新

在loadPicker中刪除if語句來檢查是否E弦等於@ 「NULL」

所以不是:

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]]; 
    } 
} 

務必:

for(NSString *pickerValue in getarray){ 
    [pickerLoaderArray addObject:pickerValue]; 
} 
+0

但是選擇器加載了除NULL之外的所有值,因此此空值將顯示在其中。 – rbk

+0

當你執行此行時,它會加載除NULL以外的所有值[[getarray objectAtIndex:j] isEqualToString:@「NULL」]你將忽略所有帶有@「NULL」值的NSString對象 – YuviGr

+0

查看更新後的答案.. – YuviGr