Swift - UITableView的用法
因为倾向于纯代码编码,所以不太喜欢可视化编程,不过也略有研究,所以项目里面的所有界面效果,全部都是纯代码编写!
终于到了重中之重的tableview的学习了,自我学习ios编程以来,工作中用得最多的就她了,所以不管是以前学习和现在学习,我都把对tableview的学习放在重点!
闲话少叙,代码如下:
一、先谈自定义cell,以及自定义cell上控件的自定义
cell是直接用xib拖拽的,很方便有木有
import UIKit
class MyCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
@IBOutlet weak var headerImg: UIImageView!
@IBOutlet weak var fileLab: UILabel!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
二、在控制器里面的调用
1)声明及初始化
class SeventViewController: UIViewController,UITableViewDelegate,UITableViewDataSource
var myTableView:UITableView?
var imgAry = [String]()
var fileAry = [String]()
func creatTableView() {
self.myTableView = UITableView(frame:self.view.frame,style:.plain)
self.myTableView?.delegate = self
self.myTableView?.dataSource = self
self.myTableView?.tableFooterView = UIView()
self.myTableView?.register(UINib.init(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "MyCell")
self.myTableView?.rowHeight =
self.view.addSubview(self.myTableView!)
}
2)添加了一个表头,具体如下
//创建一个表头标签
let headerLab = UILabel()
headerLab.frame = CGRectMake(, , SCREEN_WIDTH, )
headerLab.backgroundColor = UIColor.orangeColor()
headerLab.textColor = UIColor.whiteColor()
headerLab.numberOfLines =
headerLab.lineBreakMode = NSLineBreakMode.ByWordWrapping //换行方式
headerLab.text = "常见 UIKIT 控件"
headerLab.font = UIFont.systemFontOfSize()
self.tableView.tableHeaderView = headerLab
3)创建内容数组
self.imgAry = ["1.jpeg","1.jpeg","1.jpeg","1.jpeg","1.jpeg","1.jpeg",]
4)代理方法的实现
有2个代理方法是必须实现的
(1)返回行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.imgAry.count
}
(2)cell的初始化
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyCell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
cell.headerImg.image = UIImage(named:self.imgAry[indexPath.row])
cell.fileLab.text = "\(indexPath.row)"
return cell
}
(3)cell的点击方法
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.row {
case :
self.creatAlertView(title: "", msg: "\(indexPath.row)")
break
case :
self.creatAlertView(title: "", msg: "\(indexPath.row)")
break
case :
self.creatAlertView(title: "", msg: "\(indexPath.row)")
break
case :
self.creatAlertView(title: "", msg: "\(indexPath.row)")
break
case :
self.creatAlertView(title: "", msg: "\(indexPath.row)")
break
case :
self.creatAlertView(title: "", msg: "\(indexPath.row)")
break
default:
break
}
}
func creatAlertView(title:String,msg:String){
let hAlertView = UIAlertController(title:"温馨提示",message:"你点击了\(msg)",preferredStyle:.alert)
let cancelAction = UIAlertAction(title:"取消",style:.cancel,handler:nil)
let okAction = UIAlertAction(title:"好的",style:.default)
hAlertView.addAction(cancelAction)
hAlertView.addAction(okAction)
self.present(hAlertView, animated: true, completion: nil)
}
最终效果如下:

注:当然了,还有很多其他的方法,如果用到了,可以自己看一下!
三、在这里为tableview做一个分组,代码如下
1)声明
self.imgAry1 = ["2.jpeg","2.jpeg","2.jpeg","2.jpeg","2.jpeg","2.jpeg",]
2)添加并修改相关代理方法
func numberOfSections(in tableView: UITableView) -> Int {
return
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == {
return self.imgAry.count
}
return self.imgAry1.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
var header = NSArray()
header = ["第一区","第二区"]
return header[section] as? String
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyCell = tableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCell
if indexPath.section == {
cell.headerImg.image = UIImage(named:self.imgAry[indexPath.row])
cell.fileLab.text = "\(indexPath.section+indexPath.row)"
}else
{
cell.headerImg.image = UIImage(named:self.imgAry1[indexPath.row])
cell.fileLab.text = "\(indexPath.section+indexPath.row)"
}
return cell
}
这样的话,一个简单的分组就完成了,不过tableview博大精深,还得继续钻研啊!
效果图如下:

Swift - UITableView的用法的更多相关文章
- Swift - UITableView展开缩放动画
Swift - UITableView展开缩放动画 效果 源码 https://github.com/YouXianMing/Swift-Animations // // HeaderViewTapA ...
- Swift - UITableView状态切换效果
Swift - UITableView状态切换效果 效果 源码 https://github.com/YouXianMing/Swift-Animations // // TableViewTapAn ...
- SWIFT UITableView的基本用法
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: ...
- UITableView类用法大全:UITableView属性
[storyboard创建tableView步骤] 1.设置根视图 2.选中视图,设置导航栏editor/embed in/navigationcontroller 3.cell设置Identifie ...
- IOS SWIFT UITableView 实现简单微博列表
// // Weibo.swift // UITableViewCellExample // // Created by XUYAN on 15/8/15. // Copyright (c) 2015 ...
- Swift - UITableView里的cell底部分割线左侧靠边
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, ...
- Swift - enumerateObjectsUsingBlock的用法
enumerateobjectsusingblock:不是Array的方法在NSArray使用.如果你想使用它,你需要一个实例NSArray而不是Array. import Foundation va ...
- swift函数的用法,及其嵌套实例
import Foundation //swift函数的使用 func sayHello(name userName:String ,age:Int)->String{ return " ...
- Swift继承的用法
一个类可以继承另一个类的方法,属性和其它特性.当一个类继承其它类,继承类叫子类,被继承类叫超类(或父类).在Swift中,继承是区分「类」与其它类型的一个基本特征. 在Swift中,类可以调用和访问超 ...
随机推荐
- CSS content换行技术实现字符animation loading效果
一.之前我的字符loading实现 关于字符打点动画实现,我之前使用过box-shadow模拟,还有border+background模拟,还有使用text-shadow实现,以及今年自认为是最好的实 ...
- Dubbo阅读笔记——高级功能
事件处理线程说明 如果事件处理的逻辑能迅速完成,并且不会发起新的IO请求,比如只是在内存中记个标识,则直接在IO线程上处理更快,因为减少了线程池调度. 但如果事件处理逻辑较慢,或者需要发起新的IO请求 ...
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- 使用寄存器点亮LED——编程实战
stm32的编程和stc89c51还是存在着很多思想上的不同的,在51单片机中,我们点亮LED灯,只用给对应IO高低电平就可以了,而stm32中,就一个简单的GPIO,我们也需要配置好几个寄存器,而且 ...
- C语言一闪而过
头文件#include<stdlib.h> main函数system("pause");
- [LintCode]计算两个数的交集(一)
问题分析: 既然返回值没有重复,我们不妨将结果放进set中,然后对两个set进行比较. 问题求解: public class Solution { /** * @param nums1 an inte ...
- server后台程序的内存使用问题
眼下我开发的一个server后台程序存在这么一个问题,因为我的程序要不断的收发消息,并做统计.统计用的是stl的多重map.在统计中会不断的往map里赛数据. 可是每次统计后我都会调用clear()去 ...
- 使用子查询可提升 COUNT DISTINCT 速度 50 倍
注:这些技术是通用的,只不过我们选择使用Postgres的语法.使用独特的pgAdminIII生成解释图形. 很有用,但太慢 Count distinct是SQL分析时的祸根,因此它是我第一篇博客的不 ...
- java设计模式——多例模式
★ 缓存在单例中的使用 缓存在编程中使用很频繁,有着非常重要的作用,它能够帮助程序实现以空间换取时间,通 常被设计成整个应用程序所共享的一个空间,现要求实现一个用缓存存放单例对象的类. 说明:该 ...
- (转)一种开源的跨平台视频开发框架:VideoLAN - VLC media player
VLC原先是几个法国的大学生做的项目,后来他们把VLC作为了一个开源的项目,吸引了来自世界各国的很多优秀程序员来共同编写和维护VLC,才逐渐变成了现在这个样子.至于为什么叫VideoLan Clien ...