首先想知道如何调试问题所在,什么是无限期地停止主线程。我读到某个地方,一旦我的应用程序冻结,我应该暂停它并看到线程,我能够看到这么长时间内占用的东西。我做到了但我无法找出原因。
其次,我还应该提一下我的场景,因为冻结总是可以重复的。我点击tableViewController
(通过UILabel
)打开TapGestureRecongniser
。用户可以在tableViewController
中选择多个单元格,UILabel
可以反映用户在tableViewController
中选择的值。我有一个中央ContentManager来处理应用程序的内容。 tableViewController
更改了ContentManager
和UILabel
次提取的列表,以便自行更新。一切都很好,直到这里。如果我再次打开TableViewController
,该应用会冻结。仅当用户更改了所选单元格时才会发生这种情况。如果用户打开tableViewController
但未更改任何单元格的选定状态并按下,则应用程序在再次打开时不会冻结。这是我的tableViewController
import UIKit
import SwiftEventBus
class InterestsTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
title = "Select Your Interests"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Select", style: .Plain, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItem?.enabled = false
self.clearsSelectionOnViewWillAppear = false
setPreSelectedCells()
}
func setPreSelectedCells() {
var interestList = ContentManager.shared.getInterests()!
if interestList.count > 0 {
for i in 0..<interestList.endIndex {
let interest = interestList[i]
let indexPath = NSIndexPath(forRow: 0, inSection: i)
if interest.isSelected {
tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
self.tableView(self.tableView, didSelectRowAtIndexPath: indexPath);
} else {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
self.tableView(self.tableView, didDeselectRowAtIndexPath: indexPath);
}
}
}
}
func addTapped() {
print("interests selected")
print(tableView.indexPathsForSelectedRows?.count)
if let indexPaths = tableView.indexPathsForSelectedRows {
var selectedInterests : [String] = []
for indexPath in indexPaths {
selectedInterests.append(ContentManager.shared.getInterests()![indexPath.section].id)
}
if selectedInterests.count > 0 {
ApiCallsManager.updateInterests(selectedInterests)
navigationController?.popViewControllerAnimated(true)
}
}
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if ContentManager.shared.getInterests() != nil {
return ContentManager.shared.getInterests()!.count
} else {
return 0
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 10.0
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
let headerView: UIView = UIView()
headerView.backgroundColor = UIColor.clearColor()
return headerView
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("InterestsTableViewCell", forIndexPath: indexPath) as! InterestsTableViewCell
let interest = ContentManager.shared.getInterests()![indexPath.section]
cell.interestLabel.text = interest.getCategory()
cell.picture.kf_setImageWithURL(NSURL(string: interest.picture)!)
if cell.selected {
cell.filter.alpha = 0.85
} else {
cell.filter.alpha = 0.3
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? InterestsTableViewCell {
cell.filter.alpha = 0.85
if let count = tableView.indexPathsForSelectedRows?.count {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Select (\(count))", style: .Plain, target: self, action: #selector(addTapped))
} else {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Select", style: .Plain, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItem?.enabled = false
}
}
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? InterestsTableViewCell {
cell.filter.alpha = 0.3
if let count = tableView.indexPathsForSelectedRows?.count {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Select (\(count))", style: .Plain, target: self, action: #selector(addTapped))
} else {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Select", style: .Plain, target: self, action: #selector(addTapped))
navigationItem.rightBarButtonItem?.enabled = false
}
}
}
}
感谢。