我具有ExpandableCell的表格视图!这个tableView从互联网上加载所有数据!当它从服务器解析JSON时,以及当我搜索它给我一个正确的结果但单击单元格扩展单元格时,它的工作都很好。该应用程序崩溃了! func cellOpened(sender:UIButton)函数中发生的崩溃我该如何解决这个问题!
var aide = [AIDE]()
var filteredAide = [AIDE]()
//searchBar
var inSearchMode = false
var t_count:Int = 0
var lastCell: StackViewCell = StackViewCell()
var button_tag:Int = -1
let cellSpacingHeight: CGFloat = 5
var results: [String : AnyObject]?
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
loadQuestion()
searchBar.delegate = self
tableView.register(UINib(nibName: "StackViewCell", bundle: nil), forCellReuseIdentifier: "StackViewCell")
tableView.delegate = self
tableView.dataSource = self
tableView.allowsSelection = false
tableView.separatorStyle = .none
}//END
func loadQuestion() {
API.getAideList { (error, aide: [AIDE]?) in
if let aide = aide {
self.aide = aide
self.tableView.reloadData()
ALLoadingView.manager.hideLoadingView()
}
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == button_tag {
return 0.25*self.view.frame.height
} else {
return 0.08*self.view.frame.height
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return cellSpacingHeight
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = UIColor.clear
return headerView
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (inSearchMode) ? filteredAide.count : aide.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "StackViewCell", for: indexPath) as! StackViewCell
if !cell.cellExists {
let aideData = (inSearchMode) ? filteredAide[indexPath.row] : aide[indexPath.row]
cell.backgroundColor = UIColor.white
cell.layer.borderColor = UIColor.black.cgColor
cell.textView.text = aideData.FaqAnswer
cell.open.setTitle(aideData.FaqQuestion, for: .normal)
cell.plusButton.text = "+"
cell.open.tag = t_count
cell.openVtwo.tag = t_count
cell.open.addTarget(self, action: #selector(cellOpened(sender:)), for: .touchUpInside)
cell.openVtwo.addTarget(self, action: #selector(cellOpened(sender:)), for: .touchUpInside)
t_count += 1
cell.cellExists = true
} else {
let aideData = (inSearchMode) ? filteredAide[indexPath.row] : aide[indexPath.row]
cell.backgroundColor = UIColor.white
cell.layer.borderColor = UIColor.black.cgColor
cell.textView.text = aideData.FaqAnswer
cell.open.setTitle(aideData.FaqQuestion, for: .normal)
cell.plusButton.text = "+"
cell.open.tag = t_count
cell.openVtwo.tag = t_count
cell.open.addTarget(self, action: #selector(cellOpened(sender:)), for: .touchUpInside)
cell.openVtwo.addTarget(self, action: #selector(cellOpened(sender:)), for: .touchUpInside)
t_count += 1
cell.cellExists = true
}
UIView.animate(withDuration: 0) {
cell.contentView.layoutIfNeeded()
}
return cell
}
@objc func cellOpened(sender:UIButton) {
self.tableView.beginUpdates()
let previousCellTag = button_tag
if lastCell.cellExists {
self.lastCell.animate(duration: 0.2, c: {
self.view.layoutIfNeeded()
})
if sender.tag == button_tag {
button_tag = -1
lastCell = StackViewCell()
}
}
if sender.tag != previousCellTag {
button_tag = sender.tag
lastCell = tableView.cellForRow(at: IndexPath(row: button_tag, section: 0)) as! StackViewCell
self.lastCell.animate(duration: 0.2, c: {
self.lastCell.plusButton.text = "-"
self.view.layoutIfNeeded()
})
}
self.tableView.endUpdates()
}
let userType = currentUser.integer(forKey: "user_type")
@IBAction func backBtnPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}//END
//SearchBar
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
inSearchMode = false
searchBar.setShowsCancelButton(true, animated: true)
return true
}//END
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
inSearchMode = false
self.searchBar.endEditing(true)
searchBar.showsCancelButton = false
searchBar.resignFirstResponder()
tableView.reloadData()
}//END
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
inSearchMode = false
tableView.reloadData()
view.endEditing(true)
tableView.reloadData()
}//END
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.text = ""
inSearchMode = false
tableView.reloadData()
view.endEditing(true)
}//END
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
inSearchMode = false
tableView.reloadData()
view.endEditing(true)
} else {
inSearchMode = true
_ = searchBar.text!.lowercased()
filteredAide = aide.filter({ (aide: AIDE) -> Bool in
return aide.FaqQuestion.lowercased().contains(searchText.lowercased()) || aide.FaqAnswer.lowercased().contains(searchText.lowercased())
})
self.tableView.reloadData()
}
}//END