Swift 4.0 纯代码实现UITableView
2018-02-01 17:46
423 查看
class ViewControllerI: UIViewController,UITableViewDelegate,UITableViewDataSource { var mytableView : UITableView? let testValue:NSString = "这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容\n这是测试内容" override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.gray mytableView = UITableView(frame: CGRect(x: 0, y: 64, width: view.frame.width, height: view.frame.height) , style: .plain) mytableView?.tableFooterView = UIView() mytableView?.delegate = self // 设置代理 mytableView?.dataSource = self mytableView?.estimatedRowHeight = 60 mytableView?.rowHeight = UITableViewAutomaticDimension view .addSubview(mytableView!) //注册UITableView,cellID为重复使用cell的Identifier mytableView?.register(UITableViewCell.self, forCellReuseIdentifier: "cellID") } /* @注意:我们前边的ViewController继承了UITableViewDataSource @和 UITableViewCelegate。如果我们不注册下面的三个方法XCode就会报错!!! */ // 列数 func numberOfSections(in tableView: UITableView) -> Int { return 1 } // 行数 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } // 创建 UITableViewCell func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cells = (tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)) as UITableViewCell cells.textLabel?.text = testValue as String return cells } // 点击响应事件 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) } // 分割线无间距 override func viewDidLayoutSubviews() { mytableView?.separatorInset = .zero mytableView?.layoutMargins = .zero } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { cell.separatorInset = .zero cell.layoutMargins = .zero }
相关文章推荐
- Swift实现一句代码不用写加载首次启动引导图
- Swift语言之View,Button控件实现小方块在界面上的移动(纯代码实现)
- swift在storyboard中如何实现代理委托指向(简单代码)
- ReactiveSwift源码解析(十) Lifetime代码实现
- Swift编程中实现希尔排序算法的代码实例
- UI023---代码实现UITableView
- ReactiveSwift源码解析(十一) Atomic的代码实现以及其中的Defer延迟、Posix互斥锁、递归锁
- 用Swift写一个IOS的自定义弹窗-纯代码实现
- swift----纯代码实现 UITableViewCell单元格的自定义
- ReactiveSwift源码解析(八) SignalProducer的代码的基本实现
- ReactiveSwift源码解析(一) Event与Observer代码实现
- Swift - 纯代码实现页面segue跳转,以及参数传递
- swift4 使用DrawerController实现侧滑菜单功能的示例代码
- Swift开发:NSLayoutConstraint纯代码实现自动布局-初级篇
- 一个功能,两个平台,三种语言 -(iOS,Swift,Android)App代码实现对比篇
- Swift - 纯代码实现页面segue跳转,以及参数传递
- 关于图片压缩的代码实现Swift
- IOS Swift UITableViewcontroller实现点击空白处隐藏键盘
- Swift实现快速排序算法的代码示例
- UItableView基本步骤(代码实现中还包含快速创建数组)