2016-09-14 54 views
0

我有一個UITableView我加載了一些應用程序設置。我需要表格是單選的,並且由於表格保存了設置,因此可能有一些單元格會根據設置啓用狀態以編程方式選擇。indexPathForSelectedRow返回零,但我有一個單元格編程選擇

目前,我遇到一個奇怪的行爲,其中,如果以編程方式選擇一個單元格,然後indexPathForSelectedRow回報nil(當它應該返回我選擇的單元格的指數),因此當我在第二電池接頭(改變當前選定的設置)我最終選擇了兩個單元格(即使在我的tableView對象中將allowMultipleSelection設置爲false)。

這裏是我的代碼:

override func viewDidLoad() { 
    tableView.allowsMultipleSelection = false 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("myCell") 

    if let cell = cell { 
     // tableObject is an array containing settings model 
     let row = tableObject[indexPath.row] 
     cell.textLabel!.text = row.settingValue 
     if row.selected { 
      cell.setSelected(true, animated: false) 
      cell.accessoryType = .Checkmark 
     } 
     cell.tag = row.id 
    } 

    return cell! 
} 

override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { 
    // oldIndex is always nil for the cell I called setSelected in cellForRowAtIndexPath 
    if let oldIndex = tableView.indexPathForSelectedRow { 
     if let oldCell = tableView.cellForRowAtIndexPath(oldIndex) { 
      tableView.deselectRowAtIndexPath(oldIndex, animated: true) 
      oldCell.accessoryType = .None   
     } 
    } 

    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     cell.setSelected(true, animated: true) 
     cell.accessoryType = .Checkmark 
    } 

    return indexPath 
} 

override func tableView(tableView: UITableView, willDeselectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { 
    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     cell.accessoryType = .None 
     tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    } 
    return indexPath 
} 

任何想法,我怎麼能總是在同一時間只選擇了一個單元格?

謝謝!

+1

你需要使用DIDSELECT不將選擇和方法,你已經有了indexpath你爲什麼不喜歡tableView.indexPathForSelectedRow –

+0

@NitinGohel代碼帶上面的代碼甚至當我改變它使用didSelectRow我仍然觀察到相同的行爲 – mradzinski

+0

讓我發表一個答案給我幾個薄荷 –

回答

0

萬一別人遇到此相同的行爲,似乎穿越cell.setSelected()選擇小區是不一樣的調用tableView.selectRowAtIndexPath()方法。選擇最新的行可以完美地完成這項工作並解決問題。

0

創建像

var arrSelectCell = [NSIndexPath]() 

數組並做代碼在didSelectRowAtIndexPath類似以下內容:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     if arrSelectCell.contains(indexPath) { 
     if let oldCell = tableView.cellForRowAtIndexPath(indexPath) { 

       if let index = arrSelectCell.indexOf(indexPath) { 
        arrSelectCell.removeAtIndex(index) 
       } 
      tableView.deselectRowAtIndexPath(indexPath, animated: true) 
      oldCell.accessoryType = .None 
     } 

    } 
    else 
    { 
     arrSelectCell.append(indexPath) 
     if let selectCell = tableView.cellForRowAtIndexPath(indexPath) { 
      tableView.deselectRowAtIndexPath(indexPath, animated: true) 
      selectCell.accessoryType = .Checkmark 
     } 


    }  

,也不要忘記在cellForRowAtIndexPath設置一個代碼來檢查已檢查後未選中單元格保持重用細胞也。因此需要編寫如下幾個代碼。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

      let cell = tableView.dequeueReusableCellWithIdentifier("serchCell", forIndexPath: indexPath) as! SearchTableViewCell 

      if arrSelectCell.contains(indexPath) 
      { 
        cell.accessoryType = .Checkmark 
      } 
      else 
      { 
        cell.accessoryType = .None 
      } 

      return cell 
     } 
+0

此代碼不起作用,因爲'ArrSelectedCell'從未被初始化。爲什麼它是可選的,並以大寫字母開頭? – vadian

+0

對資本沒有任何限制,這不是一個客觀的C,所以不需要初始化。讓我告訴你截圖以及 –

+0

這不知何故,感覺像一個黑客給我。 '''indexPathForSelectedRow'''應該返回一個'''NSIndexPath'''對象,無論我使用'''setSelected'''設置我的單元格爲選中狀態,還是用戶點擊並選中它。 – mradzinski

0

請注意,致電tableView.reloadData()會將tableView.indexPathForSelectedRow重置爲零。

這裏如何通過tableView.indexPathForSelectedRow完成表格視圖單項選擇:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     cell.accessoryType = tableView.indexPathForSelectedRow == indexPath ? .checkmark : .none 
     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    { 
     if let visiblePaths = tableView.indexPathsForVisibleRows 
     { 
      for visibleIndexPath in visiblePaths 
      { 
       let cell = tableView.cellForRow(at: visibleIndexPath) 
       if visibleIndexPath == indexPath 
       { 
        cell?.accessoryType = .checkmark 
       } 
       else 
       { 
        cell?.accessoryType = .none 
       } 
      } 
     } 
    } 
相關問題