与OC的不同##
在遵循代理的方式不同,直接在viewcontroller后加上代理即可
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate
遵循的几个方法的不同##
Swift在缺省的第一个参数值时使用_ 代替
需要注意的是,Swfit相比于之前的方式简介了很多,也严谨了很多,在使用重用方法时,不允许使用nil作为参数,参数必须为string类型
下面简单看一下我的几个代理方法
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return data.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell.init(style: .default, reuseIdentifier: "reuse")
let text = data[indexPath.row]
cell.textLabel?.text = text
cell.textLabel?.textColor = UIColor.black
return cell
}
public func numberOfSections(in tableView: UITableView) -> Int
{
return 1;
}
当然viewdidload中还是要去遵循代理
override func viewDidLoad() {
super.viewDidLoad()
tableview.dataSource = self
tableview.delegate = self
}