2016-06-21 60 views
0

我有這樣的tableView:的UITableView選擇的onLoad

Image1

和它的偉大工程,我用這個來過濾從HTTP請求中接收到的數據。我想在更改視圖時保存選擇並在我返回時重置。我嘗試過(見代碼評論),但我沒有錯誤。 有人可以幫我解釋一下我該怎麼做嗎?

這是我的代碼:

// MARK: TableView 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return names.count 
} 

// Customize header 
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView { 
    let view: UIView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 18)) 
    let label: UILabel = UILabel(frame: CGRectMake(10, 5, tableView.frame.size.width, 18)) 
    label.font = UIFont.boldSystemFontOfSize(18) 
    let string: String = names[section] 
    label.text = string 
    label.textColor = .redColor() 
    label.textAlignment = NSTextAlignment.Left 
    view.addSubview(label) 
    view.backgroundColor = ColorHex().UIColorFromHex(0xEEEEEE) 
    return view 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    var number: Int! 

    switch section { 
    case 0: 
     number = 1 //reset filter 
     break 
    case 1: 
     number = statusesDict.count 
     break 
    case 2: 
     number = queuesDict.count 
     break 
    case 3: 
     number = typesDict.count 
     break 
    case 4: 
     number = severitiesDict.count 
    default: 
     break 
    } 

    return number 
} 

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

    let cell: TicketsFilterCell! = tableView.dequeueReusableCellWithIdentifier("cellTicketFilter", forIndexPath: indexPath) as! TicketsFilterCell 

    cell.delegate = self 

    if self.selectedIndexPathArray.contains(indexPath) { 
     cell.switchFilter.setOn(true, animated: false) 
    } else { 
     cell.switchFilter.setOn(false, animated: false) 
    } 


    if(indexPath.section == 0) { 

     let text = "Resetta tutti i filtri" 

     cell.textFilter.text = text 

    } else if(indexPath.section == 1) { 

     let text = statusesDict[indexPath.row]?.capitalizedString 

     cell.textFilter.text = text 

    } else if(indexPath.section == 2) { 

     let text = queuesDict[indexPath.row]?.capitalizedString 

     cell.textFilter.text = text 

    } else if(indexPath.section == 3) { 

     let text = typesDict[indexPath.row]?.capitalizedString 

     cell.textFilter.text = text 

    } else if (indexPath.section == 4) { 

     let text = severitiesDict[indexPath.row]?.capitalizedString 

     cell.textFilter.text = text 

    } 


    return cell 

} 

//change value to true for edit tableView rows 
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{ 
    return false 
} 


//show selection 
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
{ 

    if let index = self.selectedIndexPathArray.indexOf(indexPath) { 
     self.selectedIndexPathArray.removeAtIndex(index) 
    } else { 
     self.selectedIndexPathArray.append(indexPath) 
    } 

    let cell = self.tableview.cellForRowAtIndexPath(indexPath) as! TicketsFilterCell 
    cell.toggleSwitch() 


} 

func switchButtonTapped(WithStatus status: Bool, ForCell cell: TicketsFilterCell) { 
    let indexPath = self.tableview .indexPathForCell(cell) 

    if (status == true) { 

     if self.selectedIndexPathArray.indexOf(indexPath!) == nil { 
      self.selectedIndexPathArray.append(indexPath!) 
     } 


     //update dictSelected 
     switch (self.tableview.indexPathForCell(cell)!.section) { 
     case 0: 
      print("section 0, reset all filter") 
      self.dictSelectedReset.updateValue(true, forKey: String((indexPath?.row)! + 1)) 
      break 
     case 1: 
      //take data from status 
      print("section 1, value -> \(statusesDict[(indexPath?.row)!]) for row \(String(indexPath!.row))") 
      self.dictSelectedStatus.updateValue(statusesDict[(indexPath?.row)!]!, forKey: String((indexPath?.row)! + 1)) 
      break 
     case 2: 
      //take data from queue 
      print("section 2, value -> \(queuesDict[(indexPath?.row)!]) for row \(String(indexPath!.row))") 
      self.dictSelectedQueue.updateValue(queuesDict[(indexPath?.row)!]!, forKey: String((indexPath?.row)! + 1)) 
      break 
     case 3: 
      //take data from type 
      print("section 3, value -> \(typesDict[(indexPath?.row)!]) for row \(String(indexPath!.row))") 
      self.dictSelectedType.updateValue(typesDict[(indexPath?.row)!]!, forKey: String((indexPath?.row)! + 1)) 
      break 
     case 4: 
      //take data from severity 
      print("section 4, value in -> \(severitiesDict[(indexPath?.row)!]) for row \(String(indexPath!.row))") 
      self.dictSelectedSeverity.updateValue(severitiesDict[(indexPath?.row)!]!, forKey: String((indexPath?.row)! + 1)) 
      break 
     default: 
      break 
     } 

    } 
    else { 

     if let index = self.selectedIndexPathArray.indexOf(indexPath!) { 
      self.selectedIndexPathArray.removeAtIndex(index) 
     } 


     //update dictSelected 
     //if exist, delete re-tapped 

     switch (self.tableview.indexPathForCell(cell)!.section) { 
     case 0: 
      if (dictSelectedReset.keys.contains(String((indexPath?.row)! + 1))) { 
       dictSelectedReset.removeValueForKey(String((indexPath?.row)! + 1)) 
      } 
      break 
     case 1: 
      if (dictSelectedStatus.keys.contains(String((indexPath?.row)! + 1))) { 
       dictSelectedStatus.removeValueForKey(String((indexPath?.row)! + 1)) 
      } 
      break 
     case 2: 
      if (dictSelectedQueue.keys.contains(String((indexPath?.row)! + 1))) { 
       dictSelectedQueue.removeValueForKey(String((indexPath?.row)! + 1)) 
      } 
      break 
     case 3: 
      if (dictSelectedType.keys.contains(String((indexPath?.row)! + 1))) { 
       dictSelectedType.removeValueForKey(String((indexPath?.row)! + 1)) 
      } 
      break 
     case 4: 
      if (dictSelectedSeverity.keys.contains(String((indexPath?.row)! + 1))) { 
       dictSelectedSeverity.removeValueForKey(String((indexPath?.row)! + 1)) 
      } 
      break 
     default: 
      break 
     } 
    } 

,這是細胞代碼:

protocol CellProtocol : class { 
func switchButtonTapped(WithStatus status : Bool, ForCell cell : TicketsFilterCell) 
} 
class TicketsFilterCell: UITableViewCell { 

@IBOutlet var textFilter: UILabel! 
@IBOutlet weak var switchFilter: UISwitch! 

weak var delegate : CellProtocol! 

class var reuseIdentifier: String? { 
    get { 
     return "TicketsFilterCell" 
    } 
} 

override func awakeFromNib() { 
    super.awakeFromNib() 
} 


// Uncomment here to show switches that are preselected on load 

// override func setSelected(selected: Bool, animated: Bool) { 
//  super.setSelected(selected, animated: animated) 
//  self.delegate .switchButtonTapped(WithStatus: selected, ForCell: self) 
// } 



@IBAction func switchTapped(sender: UISwitch) { 

    self.delegate.switchButtonTapped(WithStatus: sender.on, ForCell: self) 

} 

// select UISwitch and change status 
func toggleSwitch() { 
    if self.switchFilter.on { 
     self.switchFilter .setOn(false, animated: true) 
    } else { 
     self.switchFilter .setOn(true, animated: true) 
    } 
    self.delegate.switchButtonTapped(WithStatus: self.switchFilter.on, ForCell: self) 
} 

} 

我真的不知道該怎麼做是正確的..

謝謝提前。

+0

你有問題不清楚。您是否嘗試在視圖處於活動狀態時保存數組並從同一數組中加載數據。 –

+0

@UmarFarooque如果你看到我以這種方式嘗試的單元代碼,但我有零錯誤,我想我需要保存和值的數組,但我不知道如何重新設置負載值,請你解釋一下? – Jigen

回答

0

你的方法應該是這樣的。

  1. 以一個陣列[說boolArr]等於

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int)

此數組[boolArr]使用應該包含你布爾按鈕的狀態的變量number的計數IE中。如果他們打開或關閉。

  1. 當調用viewwilldisappear時,保存這個[boolArr]。

  2. 加載視圖中相同數組[boolArr]中的數據將出現並在cellForRowAtIndexPath中進一步使用。

  3. 當在cellForRowAtIndexPath中加載數據時,爲nil和empty添加必要的檢查。