2015-04-01 56 views
-1

我有一個可變數組(editedServiceParts)與其中的兩個對象;我有一個約30個單元格的表格視圖。我想檢查是否有任何表視圖單元格(cell.textLabel.text)出現在可變數組中;如果他們這樣做,我想將cell.accessory設置爲複選標記。快速枚舉邏輯錯誤檢查比較UITableViewCell

修訂這是-cellForRowAtIndexPath我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

// get the timeFormat 
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
NSMutableDictionary *preferencesDict = [[userDefaults dictionaryForKey:@"preferencesDictionary"] mutableCopy]; 
int iTimeFormat = [[preferencesDict objectForKey:@"timeFormat"] intValue]; // set timeFormat 
NSMutableArray *selectedServicesParts = [NSMutableArray new]; 
NSMutableArray *editedServiceParts = [NSMutableArray new]; 
NSString *service; 



if(tableView.tag == kServicesTableView) { // services array 

    static NSString *CellIdentifier = @"apptServicesCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell 
    SingletonServicesArray *sharedServicesArray = [SingletonServicesArray sharedServicesArray]; // list of available services 
    [cell.textLabel setText:[sharedServicesArray.globalServicesArray objectAtIndex:indexPath.row]]; // move selected row to cell 

    // separate soServices.text into individual elements 
    NSString *cleanService; 
    if(soServices.text.length > 0) { 
     selectedServicesParts = [[soServices.text componentsSeparatedByString:@","] mutableCopy]; 

     for(int x = 0; x < selectedServicesParts.count; x++) { 
      cleanService = [selectedServicesParts[x] stringByReplacingOccurrencesOfString:@"," withString:@""]; 

      [editedServiceParts insertObject: cleanService atIndex: x]; 
     } 
    } 

    // now, take the editedServicesParts (w/o commas) and mark cell if appropriate 
    for (int i = 0; i < editedServiceParts.count; i++) { if ([editedServiceParts containsObject:cell.textLabel.text]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     // Remove accessory mark of recycled cells 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    } 
//  for (int i = 0; i < editedServiceParts.count; i++) { // total number of rows in list of all available services 
//    
//   for(service in editedServiceParts) { 
//    if([cell.textLabel.text isEqualToString: service]) { 
//     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
//     break; 
       } 
//   } 
//  } 

    return cell; 

} 

發生了什麼事是隻有第一個比較被人發現,雖然有另一個應該已經被發現。我擔心這裏有一個邏輯問題,而對於我來說,我沒有看到它。幫助將不勝感激!

SD

+0

爲什麼你需要外循環,重複相同的任務多次? – dasblinkenlight 2015-04-01 20:32:29

+0

內部和外部循環都在遍歷editedServiceParts。你是否打算讓內部循環迭代你的表格視圖單元格? – DanielG 2015-04-01 20:32:59

+0

'cell'永遠不會改變,所以你不斷檢查相同的單元格文本。 – 2015-04-01 20:39:03

回答

0

您可以使用containsObject:方法來避免循環乾脆:

if ([editedServiceParts containsObject:cell.textLabel.text]) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
} else { 
    // Remove accessory mark of recycled cells 
    cell.accessoryType = UITableViewCellAccessoryNone; 
} 
+0

您的代碼仍然只給我第一場比賽,忽略其他比賽...我忘了提及,這段代碼是在-cellForRowAtIndexPath ...對不起 – SpokaneDude 2015-04-01 20:39:07

+0

當用戶從tableViewCell中選擇一行時,它被複制到textField,然後將其複製到CD商店。當從CD商店中選擇該行時,它被複制到textField中,在編輯出逗號等後用於比較...(當然,希望有道理)... – SpokaneDude 2015-04-01 20:49:31

+0

你絕對是對的比賽;刪除逗號(我將其修改爲@「,」)的代碼行不起作用(一個空格被插入到除第一個之外的所有對象中......我將繼續處理這個...非常感謝你的時間...我真的很感激它(還學到了一些東西 - containsObject)... – SpokaneDude 2015-04-01 21:10:57