我有3个错误,“'NearbyVC.Type'没有确认协议'UITableViewDataSource'”在Error1,“'NearbyVC.Type'在Error2处没有名为'session'的成员”和“预期声明“在Error3。 但是,我无法弄清楚为什么会出现这样的错误。如果您知道热解决,请帮助我。
// ※Error1※
class NearbyVC: UIViewController,UITableViewDelegate,UITableViewDataSource{
@IBOutlet var listview: UITableView?
var tableData = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int{
return 1
}
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableView{
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
let rowData:NSDictionary = self.tableData[indexPath.row] as NSDictionary
cell.textLabel.text = "Row#\(indexPath.row)"
cell.detailTextLabel?.text = "Subtitle #\(indexPath.row)"
}
let url = NSURL(string:"http:~~")
let session = NSURLSession.sharedSession()
// ※Error2※
let task:NSURLSessionTask = session.dataTaskWithURL(url,completionHandler:apiHandler)
//Use a separate handler function in lieu of doing it inline for clarity
func apiHandler(data:NSData!,response:NSURLResponse!,error:NSError!){
if (error != nil) {
println("API error:\(error),\(error.userInfo)")
}
var jsonError:NSError?
var json:NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &jsonError) as NSDictionary
if (jsonError != nil) {
println("Error parsing json: \(jsonError)")
}
else{
let status:String? = json["msg"] as? String
println("servar status:\(status)")
}
}
// ※Error3※
task.resume()
}
答案 0 :(得分:0)
ERROR1:
func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableView{
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
let rowData:NSDictionary = self.tableData[indexPath.row] as NSDictionary
cell.textLabel.text = "Row#\(indexPath.row)"
cell.detailTextLabel?.text = "Subtitle #\(indexPath.row)"
}
我不会分析你在体内做什么,但是你应该返回UITableViewCell而不是UITableView
如协议中所述:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
误差2:
let url = NSURL(string:"http:~~")
let session = NSURLSession.sharedSession()
// ※Error2※
let task:NSURLSessionTask = session.dataTaskWithURL(url,completionHandler:apiHandler)
你不能在类中初始化常量,然后用它来初始化另一个,将它移动到某个函数。
错误3
修复Error2以修复Error3。
DISCLAMER:我还没有分析你编写的整个代码,我刚刚确定了编译错误的原因。