由于socket.on("reply")的作用域限制,所以我必须在函数addHandler()内移动所有表函数。
正如您所看到的,函数addHandler()确实继承了SecondViewController,并且在我移动表函数之后发生了两个错误
1)类型SecondViewController不符合协议'UITableViewDataSource‘
2)定义与之前的值冲突(这是我不理解的部分)
class SecondViewController: UIViewController, UITableViewDataSource{//error 1
override func viewDidLoad() {
super.viewDidLoad()
print("Second view loaded")
self.title = "Ranking"
addHandler()
socket.connect()
}
func addHandler()->SecondViewController{
socket.on("reply") {data, ack in
let json = JSON(data)
print(json[0].count)
let pCount:Int = json[0].count
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return data.count when data is available from server
return pCount
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {//error 2
let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as UITableViewCell
//everything refers to this
let patient = patientSample[indexPath.row] as Patient
if let cellList = cell.viewWithTag(100) as? UILabel{
cellList.text = String(indexPath.row + 1)
}
//setting up cell connection
if let sexIcon = cell.viewWithTag(101) as? UIImageView{
sexIcon.image = self.genderIcon(patient.isMale!)
}
if let nameLabel = cell.viewWithTag(102) as? UILabel{
nameLabel.text = patient.name
}
if let scoreLabel = cell.viewWithTag(103) as? UILabel{
scoreLabel.text = String(patient.score)
}
return cell
}
for var i=0; i<pCount; ++i{
let patient = Patient(id: json[0][i]["ID"].intValue, name: json[0][i]["Name"].stringValue, mileage: json[0][i]["Mileage"].doubleValue)
}
}//end of function addHandler
}
//gender icon
func genderIcon(isMale:Bool) -> UIImage{
if isMale == true{
return UIImage(named: "boy")!
}else{
return UIImage(named: "girl")!
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
发布于 2016-01-29 09:27:15
错误#1:是因为您的类SecondViewController
没有实现采用UITableViewDataSource
委托时所需的方法。这是因为tableView方法的作用域是不可访问的。
错误#2:在swift中,你不能像以前那样在函数中声明函数。
从addHandler()
方法中取出所有与TableView相关的方法。
如果您希望tableView在套接字接收到'reply‘消息时重新加载数据。使用tableview.reloadData()
。
https://stackoverflow.com/questions/35080736
复制相似问题