我有以下代码:
import UIKit
class SearchTableViewController: UITableViewController, UISearchBarDelegate{
final let urlString = "http://tvshowapi.azurewebsites.net/tvshownewsfeed"
@IBOutlet var tableView: UITableView!
var nameArray = [String]()
var favouriteCountArray = [String]()
var imgURLArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
createSearchBar()
self.downloadJsonWithURL()
}
func createSearchBar() {
let searchBar = UISearchBar()
searchBar.showsCancelButton = false
searchBar.placeholder = "Search a show"
searchBar.delegate = self
searchBar.backgroundColor = UIColor.black
searchBar.barTintColor = UIColor.black
self.navigationItem.titleView = searchBar
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.black
let searchTextField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField
searchTextField?.textAlignment = NSTextAlignment.left
}
func downloadJsonWithURL() {
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [[String:Any]] {
for tvshow in jsonObj! {
if let name = tvshow["screenName"] as? String {
self.nameArray.append(name)
}
if let cnt = tvshow["favouriteCount"] as? Int {
self.favouriteCountArray.append("\(cnt)")
}
if let image = tvshow["imageUrl"] as? String {
self.imgURLArray.append(image)
}
}
OperationQueue.main.addOperation({
self.tableView.reloadData()
})
}
}).resume()
}
func downloadJsonWithTask() {
let url = NSURL(string: urlString)
var downloadTask = URLRequest(url: (url as? URL)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20)
downloadTask.httpMethod = "GET"
URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in
let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)
print(jsonData!)
}).resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return nameArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SearchTableViewCell
cell.nameLabel.text = nameArray[indexPath.row]
cell.dobLabel.text = favouriteCountArray[indexPath.row]
let imgURL = NSURL(string: imgURLArray[indexPath.row])
if imgURL != nil {
let data = NSData(contentsOf: (imgURL as? URL)!)
cell.imgView.image = UIImage(data: data as! Data)
}
return cell
}
}
我通过Xcode获得了三个错误:
SearchTableViewController 以下是TableViewCell的代码
class SearchTableViewCell: UITableViewCell {
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var dobLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我做错了什么?
答案 0 :(得分:1)
self.first
类具有隐式UITableViewController
属性,其中包含已连接的数据源和委托。删除tableView
并使用它。
答案 1 :(得分:0)
Xcode基本上告诉您“我已经在使用该名称”。将名称更改为tableView2之类的其他名称,它将起作用,因为Xcode与类似的名称混淆,例如您不能将对象命名为“ String”,这也会产生错误。我希望将来能对某人有所帮助。