swift-UITableView的根本使用
2016-06-20 09:09
585 查看
swift-UITableView的基本使用
废话不多说了,直接贴我今天写的代码吧:如果新手有什么不懂的,可以发我邮箱。
//
// singleInfo.swift 个人信息
// Housekeeper
//
// Created by 卢洋 on
15/10/27.
// Copyright © 2015年 奈文摩尔.
All rights reserved.
//
import Foundation
import UIKit
class singleInfo:UIViewController,UITableViewDataSource,UITableViewDelegate{
var dataTable:UITableView!;
//数据表格
var itemString=["昵称","账号","性别","地区","我的爱车"]
//当前屏幕对象
var screenObject=UIScreen.mainScreen().bounds;
//页面初始化
override func viewDidLoad()
{
super.viewDidLoad();
initView();
}
/**
UI 初始化
*/
func initView(){
self.title="我的资料";
self.view.backgroundColor=UIColor.linghtGreyBg();
creatTable();
}
/**
我的资料表格初始化
*/
func creatTable(){
let dataTableW:CGFloat=screenObject.width;
let dataTableH:CGFloat=screenObject.height;
let dataTableX:CGFloat=0;
let dataTableY:CGFloat=0;
dataTable=UITableView(frame: CGRectMake(dataTableX,
dataTableY, dataTableW, dataTableH),style:UITableViewStyle.Grouped);
dataTable.delegate=self; //实现代理
dataTable.dataSource=self; //实现数据源
self.view.addSubview(dataTable);
}
//1.1默认返回一组
func numberOfSectionsInTableView(tableView: UITableView)
-> Int {
return 2;
}
// 1.2 返回行数
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int)
-> Int {
if(section
== 0){
return 1;
}else{
return 5;
}
}
//1.3 返回行高
func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath)
->CGFloat{
if(indexPath.section == 0){
return 80;
}else{
return 55;
}
}
//1.4每组的头部高度
func tableView(tableView: UITableView,
heightForHeaderInSection section: Int)
-> CGFloat {
return 10;
}
//1.5每组的底部高度
func tableView(tableView: UITableView,
heightForFooterInSection section: Int)
-> CGFloat {
return 1;
}
//1.6 返回数据源
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath)
->UITableViewCell {
let identifier="identtifier";
var cell=tableView.dequeueReusableCellWithIdentifier(identifier);
if(cell
== nil){
cell=UITableViewCell(style: UITableViewCellStyle.Value1,
reuseIdentifier: identifier);
}
if(indexPath.section == 0){
cell?.textLabel?.text="头像";
}else{
cell?.textLabel?.text=itemString[indexPath.row];
}
cell?.accessoryType=UITableViewCellAccessoryType.DisclosureIndicator;
return cell!;
}
//1.7 表格点击事件
func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath)
{
//取消选中的样式
tableView.deselectRowAtIndexPath(indexPath,
animated: true);
//获取点击的行索引
if(indexPath.row == 0){
let pushSingleInfo=singleInfo();
pushSingleInfo.hidesBottomBarWhenPushed=true; //隐藏导航栏
self.navigationController?.pushViewController(pushSingleInfo,
animated: true);
}
}
//内存警告
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning();
print("个人信息内存警告");
}
}
效果图如下:
废话不多说了,直接贴我今天写的代码吧:如果新手有什么不懂的,可以发我邮箱。
//
// singleInfo.swift 个人信息
// Housekeeper
//
// Created by 卢洋 on
15/10/27.
// Copyright © 2015年 奈文摩尔.
All rights reserved.
//
import Foundation
import UIKit
class singleInfo:UIViewController,UITableViewDataSource,UITableViewDelegate{
var dataTable:UITableView!;
//数据表格
var itemString=["昵称","账号","性别","地区","我的爱车"]
//当前屏幕对象
var screenObject=UIScreen.mainScreen().bounds;
//页面初始化
override func viewDidLoad()
{
super.viewDidLoad();
initView();
}
/**
UI 初始化
*/
func initView(){
self.title="我的资料";
self.view.backgroundColor=UIColor.linghtGreyBg();
creatTable();
}
/**
我的资料表格初始化
*/
func creatTable(){
let dataTableW:CGFloat=screenObject.width;
let dataTableH:CGFloat=screenObject.height;
let dataTableX:CGFloat=0;
let dataTableY:CGFloat=0;
dataTable=UITableView(frame: CGRectMake(dataTableX,
dataTableY, dataTableW, dataTableH),style:UITableViewStyle.Grouped);
dataTable.delegate=self; //实现代理
dataTable.dataSource=self; //实现数据源
self.view.addSubview(dataTable);
}
//1.1默认返回一组
func numberOfSectionsInTableView(tableView: UITableView)
-> Int {
return 2;
}
// 1.2 返回行数
func tableView(tableView: UITableView,
numberOfRowsInSection section: Int)
-> Int {
if(section
== 0){
return 1;
}else{
return 5;
}
}
//1.3 返回行高
func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath)
->CGFloat{
if(indexPath.section == 0){
return 80;
}else{
return 55;
}
}
//1.4每组的头部高度
func tableView(tableView: UITableView,
heightForHeaderInSection section: Int)
-> CGFloat {
return 10;
}
//1.5每组的底部高度
func tableView(tableView: UITableView,
heightForFooterInSection section: Int)
-> CGFloat {
return 1;
}
//1.6 返回数据源
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath)
->UITableViewCell {
let identifier="identtifier";
var cell=tableView.dequeueReusableCellWithIdentifier(identifier);
if(cell
== nil){
cell=UITableViewCell(style: UITableViewCellStyle.Value1,
reuseIdentifier: identifier);
}
if(indexPath.section == 0){
cell?.textLabel?.text="头像";
}else{
cell?.textLabel?.text=itemString[indexPath.row];
}
cell?.accessoryType=UITableViewCellAccessoryType.DisclosureIndicator;
return cell!;
}
//1.7 表格点击事件
func tableView(tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath)
{
//取消选中的样式
tableView.deselectRowAtIndexPath(indexPath,
animated: true);
//获取点击的行索引
if(indexPath.row == 0){
let pushSingleInfo=singleInfo();
pushSingleInfo.hidesBottomBarWhenPushed=true; //隐藏导航栏
self.navigationController?.pushViewController(pushSingleInfo,
animated: true);
}
}
//内存警告
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning();
print("个人信息内存警告");
}
}
效果图如下:
![](http://www.myexception.cn/img/2015/10/28/111635256.png)
相关文章推荐
- Swift使用FMDB操作SQLite
- Xcode创建Swift和OC的桥接文件
- Swift中用正规表达式判断String是否是手机号码
- Swift 添加自定义响应事件
- Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)
- swift一次 Attempt to present on whose view is not in the window hierarchy的解决方法
- Swift中KIF测试的特点-b
- 回答bestswifter的BAT面试问题
- Swift控制语句
- Swift编程语言中的方法引用
- Swift 学习一函数&函数属性&懒加载
- 使用 Swift 与 Kitura 构建端到端的云应用程序
- Hello,服务端 Swift
- Swift Package Manager快速上手指南(一):环境搭建
- swift 学习笔记一
- Swift下隐藏navigationBar下面那天横线
- 初探Swift 3.0的重大变化
- [Swift] UIButton 增大UIButton点击区域
- Swift通过类名创建对象
- Swift基础(七):继承、构造过程