2015-11-29 95 views
2

我想要做的是使一個數組等於另一個,但對於特定的對象。就像第二個數組包含很多對象,我希望第一個數組從第二個數組中取出特定的對象,然後在NumberOfRowsInSection中返回它。爲特定對象創建一個數組等於另一個數組

這裏是我的嘗試,但它沒有返回值:

-(NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSINteger) section { 

    NSArray *array; 
    NSString * foundString; 
    NSInteger i; 
    for (i = 0; i < [LabelFilesArray count]; i++) { 
     if ([[[LabelFilesArray objectAtIndex: i] objectForKey: @"teamSide"] isEqualToString: @"test2"]) { 
      array = [LabelFilesArray objectAtIndex: i] ObjectForKey: @"teamSide"]; //Here is my problem. 
      foundString = @"found"; 
      if ([foundString isEqualToString: @"found"]) { 
       break; 
      } 
     } 
    } 
    return array.count; //it returns nothing 
} 
+2

可以格式化你的代碼和修復縮進問題? – ozgur

+1

顯示輸入數據的例子,你想要什麼和你得到什麼 – Wain

+0

你是什麼意思?我的代碼運行完美,但數組的一部分是即時通訊有問題 – malcolm

回答

2

你需要讓事情變得簡單,所以只要你得到它選擇在你的tableview需要的數據,並將其存儲在一個第三陣列。

這允許您刪除和重新排列行,並使您的數據源方法變得簡單。

ViewController.m:

@interface ViewController() 
{ 
    NSMutableArray *_tableData; 
} 

- (void)methodThatGetsData 
{ 
    // Get data in LabelFilesArray 

    _tableData = [NSMutableArray new]; 
    for (NSDictionary *dict in LabelFilesArray) { 
     for (NSString *filter in _filterArray) { 
      if (dict[@"teamSide"] isEqualToString:filter]) { 
       [_tableData addObject:dict]; 
       break; 
      } 
     } 
    } 
    [_tableView reloadData]; 
} 

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection: (NSINteger)section { 
    [_tableData count]; 
} 

// etc. 
+0

是的。我的意思是過濾LabelFilesArray,然後返回已過濾的數組 – malcolm

+0

@malcolm確定,更新。我假定這個數組包含字典。此外'_filterArray'包含過濾器字符串數組。 – trojanfoe

+0

非常感謝你:) – malcolm

0

您可以使用NSPredicate也濾波器陣列的內容。

例子:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K = %@", key, [dict objectForKey:key]]; 
_filterArray = [LabelFilesArray filteredArrayUsingPredicate:predicate]; 
相關問題