-2

私の質問は、配列の数が 4 より大きい場合、NSMutableArrayその名前にオブジェクトが含まれているということです。latLongArray

elementArray という名前の別の配列に 4 つおきのオブジェクトを追加し、4 つ未満の場合はすべてのオブジェクトを elementArray に追加し、NSLog のすべてのデータを出力します。

私はiPhone開発に不慣れです。ここに私のコードがあります

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = selectedCell.textLabel.text;
    placeDC *placedc;
    placedc  = [dataArray objectAtIndex:[indexPath row]];
    if ([txtFrom isFirstResponder]) {

        txtFrom.text=cellText;

        NSLog(@"%@",cellText);
        originlat=placedc.latitude;
        originlng=placedc.longitude;
        NSLog(@"%@",placedc.countryFullName);
    }
    else
{
    txtTo.text=cellText;
    destinationLat=placedc.latitude;
    DestinationLng=placedc.longitude;
    DBHandler *db=[[DBHandler alloc]init];
    NSLog(@"%@",placedc.countryFullName);
    latLongArray=[db googlePlaces:originlat :originlng :destinationLat :DestinationLng];
    if ([latLongArray count]>4) {
        for (int i =0; i<=[latLongArray count]; i=i+4) {

        }
        NSLog(@"%d",[elentArray count]);
           }
   }

    myTableView.hidden=YES;
    [self.view endEditing:YES];

}
4

3 に答える 3

3

変更するだけです:

NSMutableArray *newArrayOfFourthElement = [[NSMutableArray alloc] init];
elementArray = [[NSMutableArray alloc] init];
int j = 3;
if ([latLongArray count]>4) 
{
   for (int i =0; i <= [latLongArray count]; i=i+4)
   {
      if (j < [latLongArray count])
      {
            [newArrayOfFourthElement addObject:[latLongArray objectAtIndex:j]]; // This line will add all the forth element(i.e. 4,8,12....) from your latLongArray to newArrayOfFourthElement array.
      }
      j = j + 4;
   }
}
else
{
   elementArray = [[NSMutableArray alloc]initWithArray:latLongArray];
}

NSLog("New array is %@",newArrayOfFourthElement);
NSLog("elementArray is %@",elementArray);
于 2013-01-17T09:33:25.053 に答える
0

4番目の要素を追加したい場合

NSMutableArray *elementArray = [[NSMutableArray alloc] init];

if ([latLongArray count]>4)
{
    //add 4th element mean object at index 3
    [elementArray addObject:[latLongArray objectAtIndex:3]];
}
else{
    elementArray=[latLongArray mutableCopy];
}
NSLog(@"elentArray array >> %@  and count = %d",elentArray,[elentArray count]);  

そして、4つおきの要素を追加したい場合

NSMutableArray *latLongArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
    NSMutableArray *elementArray = [[NSMutableArray alloc] init];

    if ([latLongArray count]<4)
    {
        elementArray=[latLongArray mutableCopy];
    }
    else{
        do {
            [elementArray addObject:[latLongArray objectAtIndex:3]];
            [latLongArray removeObjectsInRange:(NSRange){0, 4}];
        } while ([latLongArray count]>4);

    }

    NSLog(@"%@",elementArray);
于 2013-01-17T09:45:16.867 に答える
0
-(void) arrayExample{
    NSMutableArray *newArray = [[NSMutableArray alloc] init];

    if (_longArray.count > 4) {
        int lastObjectIndex = _longArray.count -1;
        int nextIndexToAd = 3;
        while (nextIndexToAd <= lastObjectIndex) {
            [newArray addObject:[_longArray objectAtIndex:nextIndexToAd]];
            nextIndexToAd += 4;
        }
    }
    else{
        newArray = [_longArray mutableCopy];
    }
}
于 2013-01-17T09:38:55.547 に答える