首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >将表函数移到另一个函数中后,定义与先前的值冲突

将表函数移到另一个函数中后,定义与先前的值冲突
EN

Stack Overflow用户
提问于 2016-01-29 09:14:22
回答 1查看 424关注 0票数 0

由于socket.on("reply")的作用域限制,所以我必须在函数addHandler()内移动所有表函数。

正如您所看到的,函数addHandler()确实继承了SecondViewController,并且在我移动表函数之后发生了两个错误

1)类型SecondViewController不符合协议'UITableViewDataSource‘

2)定义与之前的值冲突(这是我不理解的部分)

代码语言:javascript
运行
AI代码解释
复制
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()
}

}

EN

回答 1

Stack Overflow用户

发布于 2016-01-29 09:27:15

错误#1:是因为您的类SecondViewController没有实现采用UITableViewDataSource委托时所需的方法。这是因为tableView方法的作用域是不可访问的。

错误#2:在swift中,你不能像以前那样在函数中声明函数。

addHandler()方法中取出所有与TableView相关的方法。

如果您希望tableView在套接字接收到'reply‘消息时重新加载数据。使用tableview.reloadData()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35080736

复制
相关文章

相似问题

领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文