简体   繁体   中英

Insert element at NSMutableArray's end

I am loading an array from a web service and displaying them in picker and all works fine. My array contains random number of questions for every category of questions. Now what i want is that i wanna add one more entry "Your own question" at the end of array so that the last row of picker displays "Your own question". How can i do this

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *retval = (id)view;
    if (!retval) {
        retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
    }

   // retval.text = [idAndNameArray objectAtIndex:row];
    if([pickerType isEqualToString:@"picker"])
    {
        retval.text = [idAndNameArray objectAtIndex:row];
    }
    else
    {
        retval.text = [quesArray objectAtIndex:row];
    }
    retval.font = [UIFont boldSystemFontOfSize:14];
    retval.numberOfLines = 3;
    return retval;
}

This code only displays number of rows(questions) fetched from the web service.

EDIT 1:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{

    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
    if([pickerType isEqualToString:@"picker"])
    {
        return [idAndNameArray count];
    }
    else
    {
       return [quesArray count];
    }

}

EDIT 2:

Resolved by making another NSMutableArray with my custom object "Your own question" and then appending it with the array fetched from web service

NSMutableArray *customObj = [NSMutableArray arrayWithObject: @"test"];
 [quesArray addObjectsFromArray:customObj];

In UIPickerViewDataSource 's method called pickerView:numberOfRowsInComponent: return your array's count plus one value( return arr.count+1; ), and in the method of UIPickerVIewDelegate that you have implemented return your label with the text when the component is greater than arr.count-1 ( if(row>(arr-1)){//return the label with 'Your own question'} ). Good Luck!

EDIT:

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
    if([pickerType isEqualToString:@"picker"])
    {
        return [idAndNameArray count];
    }
    else
    {
       return [quesArray count]+1;
    }

}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *retval = (id)view;
    if (!retval) {
        retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
    }

   // retval.text = [idAndNameArray objectAtIndex:row];
    if([pickerType isEqualToString:@"picker"])
    {
        retval.text = [idAndNameArray objectAtIndex:row];
    }
    else
    {
       if(row>quesArray.count-1){
         retval.text = @"You own question";
       }
       else{
            retval.text = [quesArray objectAtIndex:row];
         }
        }
        retval.font = [UIFont boldSystemFontOfSize:14];
        retval.numberOfLines = 3;
        return retval;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM