swift创建tableview
2017-08-01 16:35
239 查看
//
// ScendViewController.swift
// SwiftTest
//
// Created by aojinrui on 2017/8/1.
// Copyright © 2017年 aojinrui. All rights reserved.
//
import UIKit
class ScendViewController:
UIViewController,UITableViewDataSource,UITableViewDelegate {
var cellArr:[String] = []
func youbiananniu() {
print("点击右边按钮");
}
override
func viewWillAppear(_ animated:
Bool) {
super.viewWillAppear(animated)
print("viewWillAppear")
}
override
func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor =
UIColor.gray
self.navigationItem.rightBarButtonItem =
UIBarButtonItem.init(title:
"右边", style:
UIBarButtonItemStyle.plain, target:
self, action:#selector(self.youbiananniu))
self.navigationItem.title =
"第二页"
for i
in 0...40 {
cellArr.append("第\(i)个单元格")
}
let table =
UITableView.init(frame:
CGRect.init(x:
0, y: 64, width:
self.view.frame.size.width,
height: self.view.frame.size.height),
style: UITableViewStyle.plain)
table.delegate =
self;
table.dataSource =
self;
self.view.addSubview(table)
}
func tableView(_ tableView:
UITableView, heightForRowAt indexPath:
IndexPath) -> CGFloat {
return
50
}
func tableView(_ tableView:
UITableView, numberOfRowsInSection section:
Int) -> Int {
return
cellArr.count
}
func tableView(_ tableView:
UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier:
"cell")
if cell==nil {
cell = UITableViewCell.init(style:
UITableViewCellStyle.default, reuseIdentifier:
"cell")
}
cell?.selectionStyle =
UITableViewCellSelectionStyle.none
cell?.textLabel?.text =
cellArr[indexPath.row];
cell?.backgroundColor =
UIColor.red
return cell!
}
func tableView(_ tableView:
UITableView, didDeselectRowAt indexPath:
IndexPath) {
print("点击了\(indexPath.row)");
}
override
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
// ScendViewController.swift
// SwiftTest
//
// Created by aojinrui on 2017/8/1.
// Copyright © 2017年 aojinrui. All rights reserved.
//
import UIKit
class ScendViewController:
UIViewController,UITableViewDataSource,UITableViewDelegate {
var cellArr:[String] = []
func youbiananniu() {
print("点击右边按钮");
}
override
func viewWillAppear(_ animated:
Bool) {
super.viewWillAppear(animated)
print("viewWillAppear")
}
override
func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor =
UIColor.gray
self.navigationItem.rightBarButtonItem =
UIBarButtonItem.init(title:
"右边", style:
UIBarButtonItemStyle.plain, target:
self, action:#selector(self.youbiananniu))
self.navigationItem.title =
"第二页"
for i
in 0...40 {
cellArr.append("第\(i)个单元格")
}
let table =
UITableView.init(frame:
CGRect.init(x:
0, y: 64, width:
self.view.frame.size.width,
height: self.view.frame.size.height),
style: UITableViewStyle.plain)
table.delegate =
self;
table.dataSource =
self;
self.view.addSubview(table)
}
func tableView(_ tableView:
UITableView, heightForRowAt indexPath:
IndexPath) -> CGFloat {
return
50
}
func tableView(_ tableView:
UITableView, numberOfRowsInSection section:
Int) -> Int {
return
cellArr.count
}
func tableView(_ tableView:
UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier:
"cell")
if cell==nil {
cell = UITableViewCell.init(style:
UITableViewCellStyle.default, reuseIdentifier:
"cell")
}
cell?.selectionStyle =
UITableViewCellSelectionStyle.none
cell?.textLabel?.text =
cellArr[indexPath.row];
cell?.backgroundColor =
UIColor.red
return cell!
}
func tableView(_ tableView:
UITableView, didDeselectRowAt indexPath:
IndexPath) {
print("点击了\(indexPath.row)");
}
override
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
相关文章推荐
- Xcode 7.3环境下Swift 创建TableView
- Swift UITableView相关功能(一)创建tableView
- Swift - 实现表格tableViewCell里嵌套collectionView(附样例)
- iOS开发实战教学:在Swift怎样创建CocoaPod?
- The Swift Code之UILabel的创建,以及不同的状态和外观
- Swift项目使用SWTableViewCell
- swift中表视图的创建
- Swift 语言概览 -自己在Xcode6 动手写2-tableView
- 1、创建第一个swift程序(系统是OS X 10.11,工具是Xcode7.1)
- SWIFT——数组的创建和初始化
- swift创建第一个UIButton 并增加点击事件
- 造个类型不是梦-白话Swift类型创建
- swift 创建第一个UIAlertView 和UIActionSheet
- swift单例创建的几种方法
- Swift3-创建Playground
- 基于 Swift 创建 CocoaPods 完全指南
- Swift -- 创建空数组和空字典
- RXSwift:Observable的创建和订阅
- Swift - 代码创建NSLayoutConstraint布局
- Swift - 代码创建NSLayoutConstraint布局