美文网首页
欢迎来到TableView的天地

欢迎来到TableView的天地

作者: 石头7733 | 来源:发表于2016-11-28 11:40 被阅读0次

swift

梦想起航的地方

UITableView

  1. UITableView继承于UIScrollView
  2. UITableView的每一条数据对应的单元格叫cell,是UITableView的一个对象,继承于uiview
  3. UITableView可以分区显示,每个分区为section
  4. UITableView的每一行叫做raw
  • 定义一个UITableView的变量
    <pre>
    var tableView : UITableView? = nil
    </pre>

  • 初始化frame的值

<pre>
let frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
self.tableView = UITableView(frame: frame, style: .plain)
self.view.addSubview(tableView!)
</pre>

  • 要实现UITableView中的方法,必须写delegate、dataSource

<pre>
self.tableView?.delegate = self
self.tableView?.dataSource = self
</pre>

  • 要写方法之前,ViewController要继承UITableViewDelegate,UITableViewDataSource
  • 这时我们发现报错了,要把UITableViewDataSource的两个方法实现一下

<pre>
//每个分组有多少行
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 10
}
//返回单元格(每个单元格走一次)
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
//初始化单元格
let cell = UITableViewCell(style: .default, reuseIdentifier: "1")
return cell
}
</pre>

  • 这时的cell没有显示内容,我们要在返回单元格的方法中添加内容

<pre>
cell.imageView?.image = UIImage(named: "1")
cell.textLabel?.text = "zhangsan"
cell.detailTextLabel?.text = "简介"
</pre>

  • 显示结果:
  • 设置分组
    • 要把self.tableView = UITableView(frame: frame, style: .plain)中的样式改为.grouped

<pre>
//返回多少分组
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
</pre>

  • 显示单元格的行高

<pre>
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
</pre>

  • 返回头部标题

<pre>
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "标题"
}
</pre>

  • 返回头部视图的高度

<pre>
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
</pre>

  • 显示结果:
送给你

年轻,就要有上路的渴望,要与勇敢同行。被动承受,不如勇敢地面对;鸟宿檐下,不如击翅风雨;卓越是勇敢的成果。你不勇敢,没人替你坚强,能战胜束缚自己的心魔,才是真正的勇敢者。有时勇敢不利于思考,但有利于实干,所以,毅力智慧谦虚与平凡是真正勇敢者的尺度。

相关文章

  • 欢迎来到TableView的天地

    swift UITableView UITableView继承于UIScrollView UITableView的...

  • 糊涂小天使――欢迎来到她的魔法天地

    看完了,100集看着挺多的,没想到这么快。 离家出走的单纯可爱的音乐青年,最后穿上西装,梳起背头,竟然也扛起了家庭...

  • 课程前调频/40天早课(1.7)

    欢迎大家! 我们在天地间排列,欢迎您移步来到2021年我们的新家。 这个群将会成为2021年的共修群,我们在天地间...

  • 课程前调频_40天瑜伽课

    欢迎大家!我们在天地间排列,欢迎您移步来到2021年我们的新家。这个群将会成为2021年的共修群,我们在天地间流动...

  • 你知道喜欢的女孩子和别人谈恋爱是什么感觉吗

    当暮色降临,落日弥漫,天地趋于朦胧,你有故事要讲吗? 欢迎来到暮色的故事。 欢迎喜欢的读者加入青枫山...

  • 欢迎来到同人文之天地——同人文专栏!

    精彩的二创坑不能得到鉴赏的明眼? 有好的圈子要跟大家分享? 那就来同人文专栏吧! 创建初衷 现在有很多学生创作者对...

  • 欢迎来到地狱

    题目地址 解压压缩包,得到三个文件:地狱伊始.jpg、第二层地狱.docx、快到终点了.zip。jpg图片打开是空...

  • 欢迎来到1958

    1958,就是钢!-摄于贵州贵阳

  • 欢迎你来到人间

    哭声震荡了四面的风 鸟语伴奏着花香 在初春阳光明媚中舞 时间老人准备好了厚重的三万天 空间大人准备好了浪漫的诗和远...

  • 欢迎来到天堂

    (1) “泉宇您好,欢迎您来到天堂。”白衣守门人站在一道紧锁的大门前微笑的说道。 “我,叫泉宇?我死了?这里是天堂...

网友评论

      本文标题:欢迎来到TableView的天地

      本文链接:https://www.haomeiwen.com/subject/bxkopttx.html