2015-03-08 67 views
0

我需要獲得UITableView單元格的可尋址性,因此如果用戶以前選擇了該行,我可以設置複選標記。最初,用戶檢查tableViewCell,然後將其移至textField並將其存儲在Core Data中。現在,如果用戶想要更改UITableView中的某些內容,我希望能夠顯示已經選中或未選中的內容。所以,我有一個UITableView,我在一個UIPopover(-cellForRowAtIndexPath)之外;如果一個特定的單元格的文本值等於一個NSArray中的元素,我想設置單元格的附件類型進行檢查。這是我的修訂代碼:無法添加UITextViewCell的附件類型

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

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

    static NSString *CellIdentifier = @"servicesCell"; // servicesCell is just an identifier; not used that I can tell 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:CellIdentifier]; 

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

    // Configure the cell 
    SingletonServicesArray *sharedServicesArray = [SingletonServicesArray sharedServicesArray]; // initialize 

    [cell.textLabel setText:[sharedServicesArray.globalServicesArray objectAtIndex:indexPath.row]]; 

    if(![soServices hasText]) { // no text in text box 
     for (int i = 0; i < sharedServicesArray.globalServicesArray.count; i++) { 
      NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0]; 

      // if row has been checked, remove the check 
      UITableViewCell *cell = [servicesTableView cellForRowAtIndexPath:path]; 
      if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
       cell.accessoryType = UITableViewCellAccessoryNone; 
       break; 
      } 
     } 
    } 
    else { // not empty 
     for (int i = 0; i < sharedServicesArray.globalServicesArray.count; i++) { 
      NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0]; 
      UITableViewCell *cell = [servicesTableView cellForRowAtIndexPath:path]; 

      // if row is in soServices textfield, add a checkmark 
      if ([soServices.text containsString: sharedServicesArray.globalServicesArray[i]]) { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
       break; 
      } 
     } 
    } 

    return cell; 

} 

它不工作;我知道肯定至少有兩個服務在陣列中,所以我應該有兩個帶有複選標記的單元格;我得到zilch !.我如何得到這個工作正確?

+1

凡(來講控制器生命週期)是否執行了代碼塊?另外,在'-tableView:cellForRowAtIndexPath:'內部設置單元格的附件類型不會更有意義。另外,一個斷點將有助於檢查'cell'(記住在ObjC發送消息到'nil'有效) – Alladinian 2015-03-08 21:35:31

+1

你是否在更新單元格('[self.tableview reloadData]')後重新加載表視圖?就像Alladinian提到的那樣,這種類型的檢查在你的'-tableView:cellForRowAtIndexPath:'方法中會更好,以確保單元的屬性被正確設置,並且在重用單元時不會被覆蓋。 – timgcarlson 2015-03-10 14:50:34

+0

我假設你說你不能,因爲你不能訪問聲明'-tableView:cellForRowAtIndexPath:'的類中的數組。如果是這樣的話,我建議簡單的解決方案,給這個類的數組訪問權限來檢查'-tableView:cellForRowAtIndexPath:'(在這裏你可以添加一個else條件來刪除複選標記,以便它不在小區重用的情況)。然後在你的popover中,你可以重新加載tableview。 – timgcarlson 2015-03-10 17:34:34

回答

1

如果我得到它正確,這cellForRowAtIndexPath正在更新不同的tableView的單元格。將上面的代碼添加到需要更新的tableView的cellForRowAtIndexPath會更符合邏輯。

如果你想更新的tableView的細胞,然後將下面的代碼是行不通的:

for (int i = 0; i < sharedServicesArray.globalServicesArray.count; i++) { 
     NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0]; 
     UITableViewCell *cell = [servicesTableView cellForRowAtIndexPath:path]; 

     // if row is in soServices textfield, add a checkmark 
     if ([soServices.text containsString: sharedServicesArray.globalServicesArray[i]]) { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      break; 
     } 
    } 

相反,你可以這樣寫:

for (int i = 0; i < sharedServicesArray.globalServicesArray.count; i++) { 
    // if row is in soServices textfield, add a checkmark 
    if ([soServices.text containsString: sharedServicesArray.globalServicesArray[i]]) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     break; 
    } 
} 
+0

非常感謝你......我會解決它... SD – SpokaneDude 2015-03-19 15:05:50