【发布时间】:2017-03-24 07:48:31
【问题描述】:
我有以下代码:
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 出现三个错误:
- 不能用存储的属性“tableView”覆盖
- 带有 Objective-C 选择器“tableView”的“tableView”的 Getter 与具有相同 Objective-C 选择器的超类“UITableViewController”中的“tableView”的 getter 冲突
- 带有 Objective-C 选择器“setTableView:”的“tableView”设置器与具有相同 Objective-C 选择器的超类“UITableViewController”中的“tableView”设置器冲突
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
}
}
我做错了什么?
【问题讨论】:
-
A
UITableViewController已经有一个tableView属性,并且它是“自动”连接的。不需要定义自己的tableView属性。比较stackoverflow.com/questions/34565570/…。 -
只需用@IBOutlet var ttvshowTbl 重命名tableView:UITableView!并从 UITableViewCell 中删除“弱”。现在清理并运行
-
tableView 是apple api 的内置属性,它实际上和你的name 和Apple 属性冲突,你只需要更改属性的名称,清理你的项目并再次运行该项目。