【发布时间】:2018-07-28 06:19:37
【问题描述】:
我在使用泛型函数的完成处理程序方面遇到问题,它似乎不会在 UITableView 上传递任何东西,除非我在上面设置一些断点,请检查我的代码如下:
public func requestGenericData<T: Decodable>(urlString: String, httpMethod: String?, token: String!, completion: @escaping(T) ->()) {
let fullStringUrl = url + urlString
guard let url = URL(string: fullStringUrl) else { return }
guard let token = token else { return }
var urlRequest = URLRequest(url: url)
urlRequest.setValue("application/json", forHTTPHeaderField: "accept")
urlRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
urlRequest.httpMethod = httpMethod
URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
if self.isInternetAvailable() {
guard let data = data else { return }
if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode >= 200 && httpResponse.statusCode < 300 {
do {
let obj = try JSONDecoder().decode(T.self, from: data)
completion(obj)
} catch {
print("Error: \(String(describing: error))\n StatusCode: \(httpResponse.statusCode)")
}
}
}
} else {
showAlert(title: "No Internet Connect", message: "Please open your network and try again.", alertStyle: .alert, buttonTitle: "OK", buttonStyle: .default)
return
}
}.resume()
}
这个函数将在下面的表格视图代码中显示它的结果列表:
func listOfServicesMenus() {
var jobsInCategory = [String]()
apiHelper.requestGenericData(urlString: "nothing/more/than/a/noob", httpMethod: "GET", token: token) { (noobs: Noobs) in
for job in jobs.jobCategories {
jobsInCategory.append(job.name)
for jobDetails in job.getJobs {
jobsInCategory.append(jobDetails.name)
}
}
self.listOfServices.dropView.dropDownOptions = jobsInCategory
}
}
对于 swift 和开发 iOS 的新手,似乎每当我在其上设置断点时它似乎工作正常,但如果不是,它什么也不显示?
是否有人知道如何使用泛型实现正确的完成处理程序,或者我在尝试编写此代码时遗漏了什么?
感谢那些帮助我解决这个问题的人。
更新
我使用UIView、UITableViewDelegate、UITableViewDataSource、UITableView 创建了一个所谓的DropDownMenu(它是在UIView 下创建的,用于正确处理约束)。
更新 2
添加了UITableView的实现(在UIView下)
class dropDownView: UIView, UITableViewDelegate, UITableViewDataSource {
var dropDownOptions = [String]()
var tableView = UITableView()
var delegate: DropDownDelegate!
override init(frame: CGRect) {
super.init(frame: frame)
self.layer.backgroundColor = UIColor.clear.cgColor
self.backgroundColor = UIColor.clear
tableView.delegate = self
tableView.dataSource = self
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.layer.cornerRadius = 10
tableView.backgroundColor = UIColor(displayP3Red: 166/255, green: 203/255, blue: 69/255, alpha: 1.0)
self.addSubview(tableView)
tableView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
tableView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
tableView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let cell = UITableViewCell()
cell.contentView.backgroundColor = UIColor.clear
return dropDownOptions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = dropDownOptions[indexPath.row]
cell.textLabel?.textColor = UIColor(displayP3Red: 166/255, green: 203/255, blue: 69/255, alpha: 1.0)
cell.layer.borderColor = UIColor(displayP3Red: 112/255, green: 112/255, blue: 112/255, alpha: 1.0).cgColor
cell.backgroundColor = UIColor(displayP3Red: 254/255, green: 252/255, blue: 215/255, alpha: 1.0)
cell.selectionStyle = .none
cell.textLabel?.textAlignment = .center
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell: UITableViewCell = tableView.cellForRow(at: indexPath)!
selectedCell.contentView.backgroundColor = UIColor(displayP3Red: 133/255, green: 178/255, blue: 56/255, alpha: 1.0)
selectedCell.textLabel?.textColor = UIColor.white
self.delegate.dropDownPressed(string: self.dropDownOptions[indexPath.row])
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cellToDeSelect:UITableViewCell = tableView.cellForRow(at: (indexPath))!
cellToDeSelect.contentView.layer.borderColor = UIColor(displayP3Red: 112/255, green: 112/255, blue: 112/255, alpha: 1.0).cgColor
cellToDeSelect.contentView.backgroundColor = UIColor(displayP3Red: 254/255, green: 252/255, blue: 215/255, alpha: 1.0)
cellToDeSelect.textLabel?.textColor = UIColor(displayP3Red: 166/255, green: 203/255, blue: 69/255, alpha: 1.0)
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.contentView.backgroundColor = UIColor.clear
}
【问题讨论】:
-
设置`self.listOfServices.dropView.dropDownOptions = jobsInCategory`后尝试重新加载tableView
-
可能不相关,但在
dataTask返回后检查isInternetAvailable是荒谬的。 -
@AnilVarghese 无法访问 reloadData()。你介意检查我的代码以实现
UITableView。 -
PS:并且在
numberOfRowsInSection中创建一个新单元格完全没有效果,也是荒谬的。
标签: ios swift completionhandler