Xcode9学习笔记45 - UITableView滑动到指定单元格
2017-11-09 07:36
288 查看
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {//表格视图数据源协议、表格视图代理协议
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]//创建数组作为表格的数据来源
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rect = CGRect(x: 0, y: 40, width: 320, height: 420)//创建一个显示区域
let tableView = UITableView(frame: rect)//初始化一个表格视图,并设置其位置和尺寸
tableView.delegate = self//设置表格视图的代理为当前的视图控制器类
tableView.dataSource = self//设置表格视图的数据源为当前的视图控制器类
let indexPath = IndexPath(row: 11, section: 0)//初始化一个索引路径对象,用来标识表格中第一段第十二行的位置
//调用表格对象的滚动到指定位置的方法,表格将以动画的方法,滑动到指定的索引位置
tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)
self.view.addSubview(tableView)//将表格视图添加到当前视图控制器的根视图中
}
//添加一个代理方法,用来设置表格视图拥有的行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return months.count//数组的长度
}
//添加一个代理方法,用来初始化或复用表格视图中的单元格
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "reusedCell"//创建一个字符串,作为单元格的复用标识符
//单元格的标识符可以看作是一种复用机制,此方法可以从所有已经开辟内存的单元格里面,选择一个具有同样标识符的、空闲的单元格
var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
//如果在可重用单元格队列中,没有可以重复使用的单元格,则创建新的单元格。新单元格拥有一个复用标识符
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: identifier)
}
let rowNum = indexPath.row//获取当前单元格在段落中的行数
cell?.textLabel?.text = months[rowNum]//根据当前单元格的行数,从数组中获取对应位置的元素,作为当前单元格的标题文字
return cell!//返回设置好的单元格对象
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
相关文章推荐
- IOS学习之路六(UITableView滑动删除指定行)
- IOS学习之路六(UITableView滑动删除指定行)
- UITableView 单元格自定义个别不能滑动删除
- IOS学习之路六(UITableView滑动删除指定行)
- IOS学习之路六(UITableView滑动删除指定行)
- UITableViewCell,UITableView默认选中一个单元格
- uitableview刷新指定section 或 刷新指定 cell (ios)
- (iOS)UITableView只允许部分cell支持滑动删除
- uitableview 跳转指定区域
- UIKit框架(21)UITableView实现复杂单元格(一)
- [iOS]UITableView滑动删除按钮样式改变
- iOS开发-UI控件:让UITableView自动滑动(定位)到某一行cell
- UITableView的横向滑动
- UITableView滑动卡顿解决方案
- UITableView Section标题不滑动问题
- 在UITableView中插入或者删除指定的行(或者节)
- 使用 UITableView 创建表格应用演练(4)——自定义单元格
- iOS单方向禁止UITableView滑动,禁止向上或向下滑动
- iOS UITableView 移除单元格选中时的高亮状态
- 关于UITableView如何跳转到最后一行或者任意指定行