swift UITableView的基础用法

import  UIKit

import  Foundation

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

      let  data         =NSArray(contentsOf:URL(fileURLWithPath:Bundle.main.path(forResource:"MyPlist", ofType:"plist")!))


    override func viewDidLoad() {

          super.viewDidLoad()

          let  tableView =UITableView(frame:CGRect(x:0, y:40, width:self.view.frame.size.width, height:500), style:UITableViewStyle.plain)

          tableView.backgroundColor = UIColor.brown

          tableView.dataSource=self

          tableView.delegate=self

          tableView.tableFooterView=UIView()

          self.view.addSubview(tableView)

          tableView.register(UITableViewCell.self, forCellReuseIdentifier:"cellId")

    }

    func  numberOfSections(in tableView:UITableView) ->Int{

          return  (data?.count)!

    }


    func  tableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{

          let  row =data![section]as!NSArray

          return  row.count

    }


    func  tableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{

          var  cell = tableView.dequeueReusableCell(withIdentifier:"cellId", for: indexPath)

           if  cell==nil{

                 cell =UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier:"cellId")

           }

           let row=data![indexPath.section]as!NSArray

           cell.textLabel?.text= row[indexPath.row]as?String

           cell.imageView?.image=UIImage.init(named:"11")

           return  cell

    }


    func  tableView(_tableView:UITableView, heightForHeaderInSection section:Int) ->CGFloat{

            if  section ==0{

                  return  0

            }

           return  50

    }

    func  tableView(_tableView:UITableView, viewForHeaderInSection section:Int) ->UIView? {

           let  headerView =UIView()

           headerView.backgroundColor = UIColor.cyan

           return  headerView

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 13,673评论 3 3
  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,918评论 8 265
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,858评论 1 32
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 7,346评论 1 14
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,971评论 3 38

友情链接更多精彩内容