2016-09-14 23 views
0

一個小上下文 - 我有一個名爲SelectedListItemsViewController的ViewController,它具有一個由名爲selectedListItems的Realm模型對象組成的數組填充的TableView。有一個Add bar按鈕項導航到另一個名爲AllListItemsViewController的ViewController,它具有一個由名爲allListItems的Realm Model Objects數組填充的TableView,每個單元包含一個UISwitch。使用UISwitch從陣列中刪除對象

上述兩個數組都基於相同的類,它具有名爲isSelected的布爾屬性。在我的代碼中,我現在已經設置好了,所以當我在AllListItemsViewController的單元格中切換UISwitch時,如果它變爲'on',的屬性的allListItems更改爲true,並且對象被追加到selectedListItems陣列。這部分似乎工作正常。

撥動開關到「斷開」引起allListItemsindexPath.rowisSelected屬性改變到false和對象從selectedListItems其索引處被去除。這在大多數情況下都適用,但有時候會按錯誤的順序切換單元格的UISwitch(如果它們在索引0處開啓,然後是1,然後是2,然後嘗試將索引1'關閉'),那麼它會使仿真崩潰,大概是由於索引超出範圍。

在編程方面我非常業餘,所以我確信下面的代碼是草率的,我懷疑我是以最好/最有效的方式實現我的目標。

// The following arrays are for use with UISegmentedControl (other three removed for brevity) 
// This is an array of 'Appearance' list items created from allListItems 
let appearanceArray = allListItems.filter{ 
    $0.category.rangeOfString("Appearance") != nil 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: 
    NSIndexPath) -> UITableViewCell { 
    let cellIdentifier = "Cell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! AllListItemsTableViewCell 

    // This is a string value for the cell at indexPath.row 
    var listItemInCell = "" 

    // This is an array of all listItems in allListItems, and used with indexOf to check boolean status of isSelected inside of allListItems array 
    let arrayAll = allListItems.map{($0.listItem)} 

    // This is an array of all listItems in selectedListItems, and used to track remove indexes 
    let arraySelected = selectedListItems.map{($0.listItem)} 


    switch(segmentedControl.selectedSegmentIndex) 
    { 

    case 0: // Case 0 of 4, the others removed for brevity of this question 

     listItemInCell = appearanceArray[indexPath.row].listItem 

     // Index of listItemInCell inside of the array of allListItems' listItems 
     let indexOf = arrayAll.indexOf(listItemInCell)! 

     cell.listItemLabel.text = listItemInCell 

     // Tracks UISwitch activity 
     cell.callback = { (tableViewCell, switchState) in 
      if self.tableView.indexPathForCell(tableViewCell) != nil { 

       // do something with index path and switch state 
       if switchState == true { 
        allListItems[indexOf].isSelected = true 
        selectedListItems.append(self.appearanceArray[indexPath.row]) 
       } else { 
        allListItems[indexOf].isSelected = false 
        let indexInSelected = arraySelected.indexOf(listItemInCell)! 
        selectedListItems.removeAtIndex(indexInSelected) 
       } 
      } 
     } 

     if appearanceArray[indexPath.row].isSelected { 
      cell.toggleIsSelected.on = true 
     } else { 
      cell.toggleIsSelected.on = false 
     } 

     break 

有沒有的要對這個不具備優勢的情況下,導致運行錯誤更好的辦法?我的一個想法是,記錄selectedListItems索引可能不是最好的路由,因爲它是一個全局變量,並且是懶散計算的,所以它並不總是最新的。另一個想法是,當創建對象屬性的數組來追蹤索引時,某些東西在翻譯時會丟失,而不是能夠在對象數組本身中找到給定點的索引。

回答

0

在過去的3個小時裏徘徊了一陣之後,我發現了它。在我的switchState其他情況下,而不是使用

    let indexInSelected = arraySelected.indexOf(listItemInCell)! 
        selectedListItems.removeAtIndex(indexInSelected) 

我代替

    let objectToRemove = self.appearanceArray[indexPath.row] 

        for object in selectedListItems { 
         if object == objectToRemove { 
          selectedListItems.removeAtIndex(selectedListItems.indexOf(objectToRemove)!) 
         } 
        } 

這似乎已經完全固定索引超出範圍了我的問題,我有一些刪除的情況下有。它也看起來像我可以擺脫我已經支持原來的兩行不必要的代碼。