And*_*485 13 uitableview ios swift
嗨,我想填充这个tableview
我不使用静态表视图,因为我收到错误,我决定使用动态表视图并禁用表中的滚动.表视图仅用于以下信息:
@IBOutlet var tableInfoQuake: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = quake.place
tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.textLabel?.text = quake.place
tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.textLabel?.text = "Mag: \(quake.mag)"
tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.textLabel?.text = "Cordinate: (\(quake.longitude),\(quake.latitude))"
tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.textLabel?.text = "Depth: \(quake.depth)"
Run Code Online (Sandbox Code Playgroud)
但在表格视图中,我没有任何可视化.我能怎么做?谢谢.
编辑:我这样做修改每一行:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellIdentifier = "quakeInfo"
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell
if indexPath == NSIndexPath(forRow: 0, inSection: 0){
cell.textLabel!.text = quake.place
cell.detailTextLabel!.text = "Mag: \(quake.mag)"
}
if indexPath == NSIndexPath(forRow: 1, inSection: 0){
cell.textLabel!.text = "Cordinate: (\(quake.longitude),\(quake.latitude))"
cell.detailTextLabel!.text = "Depth: \(quake.depth)"
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
zru*_*nst 36
因此,您希望动态填充tableview.嗯......在这里,你去找我的朋友:
好的....所以你知道协议是什么?好吧,如果你不... BAM,现在你做(不是我的视频).DataSource是UITableView将调用以检索它需要显示的数据的内容.具体而言,有问题的DataSource是UITableViewDataSource.
所以...
// change top to...
class MyViewController:UIViewController, UITableViewDataSource
Run Code Online (Sandbox Code Playgroud)
这使得UITableViewDataSource成为ViewController的一项要求.接下来,您需要在UITableView上设置数据源.所以....
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = quake.place
tableInfoQuake.datasource = self
}
Run Code Online (Sandbox Code Playgroud)
假设您现在要在表格中添加7个单元格,并且所有7个单元格都会显示"Hello Man".对于这个例子,我将采用最简单但最简单的方式.如果你想让我更深入,只需在下面评论,我会做得更好.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let label = UILabel(CGRect(x:0, y:0, width:200, height:50))
label.text = "Hello Man"
cell.addSubview(label)
return cell
}
Run Code Online (Sandbox Code Playgroud)
这就是第一步.我们已经告诉桌子,ViewController在数据方面有它正在寻找的东西,我们已经返回了数据供它使用.
与步骤1非常相似,此步骤首先添加一些内容...
// change top to...
class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate
Run Code Online (Sandbox Code Playgroud)
然后,再次,就像顶部一样,我们将编辑viewDidLoad()函数...
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = quake.place
tableInfoQuake.datasource = self
tableInfoQuake.delegate = self
}
Run Code Online (Sandbox Code Playgroud)
现在我们必须实现指定每个单元格高度的委托方法.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
Run Code Online (Sandbox Code Playgroud)
我们现在已将该委托添加到表中,并实现了告诉UITableView每个单元格高度的方法.
class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableInfoQuake: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableInfoQuake.datasource = self
tableInfoQuake.delegate = self
}
// UITableViewDataSource Functions
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 7
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let label = UILabel(CGRect(x:0, y:0, width:200, height:50))
label.text = "Hello Man"
cell.addSubview(label)
return cell
}
// UITableViewDelegate Functions
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
}
Run Code Online (Sandbox Code Playgroud)
祝你好运.
ZR
归档时间: |
|
查看次数: |
17826 次 |
最近记录: |