2013-01-17 95 views
-2

我的問題是我有一個NSMutableArraylatLongArray包含對象的名稱,如果數組數大於4我如何獲得數組的每個第四個元素?

我要添加每4個對象與名稱elementArray另一個數組,如果小於4要添加elementArray中的所有對象,並打印NSLog中的所有數據。

任何機構都可以幫助我解決這個問題。 這裏是我的代碼

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

} 
+0

不工作元素數組0 0 – Saleem

回答

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); 
+0

不工作元素數組cout 0 – Saleem

+0

+1爲我工作 – doge

+0

@Da_smokes謝謝:) Saleem最後nslog給你0? – Rushi

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

如果你想添加第四個元素

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

如果你想添加的每第四個元素

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); 
+0

我想添加每第四個元素 – Saleem

+0

,所以它會添加第四個元素。這有什麼問題? – Rajneesh071

+0

謝謝@Saleem如果你有任何問題,然後問:) – Rajneesh071

相關問題