Swift基础--使用TableViewController自己定义列表
首先建立一个swift项目,把storyboard的内容删掉,加入一个Navigation Controller。然后设置storyboard相应界面的class,在Navigation Controller界面设置View Controller的is initial View Controller,这里使用的自己定义列表内容。所以要新建一个继承UITableViewCell的类,然后设置storyboard中Table View的Prototype Cells的class,对于点击item进入详情界面,使用TableView 中的prepareForSegue方法。
JieTableViewController.swift
//
// JieTableViewController.swift
// JieTableView
//
// Created by jiezhang on 14-10-5.
// Copyright (c) 2014年 jiezhang. All rights reserved.
// import UIKit class JieTableViewController: UITableViewController { var listVideos : NSMutableArray! override func viewDidLoad() {
super.viewDidLoad()
var bundle = NSBundle.mainBundle()
let plistPath : String! = bundle.pathForResource("videos", ofType: "plist")
listVideos = NSMutableArray(contentsOfFile: plistPath)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//以下这段话是设置左边编辑。右边加入item self.navigationItem.leftBarButtonItem = self.editButtonItem()
let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "insertNewObject:")
self.navigationItem.rightBarButtonItem = addButton } func insertNewObject(sender: AnyObject) {
var item : NSDictionary = NSDictionary(objects:["http://c.hiphotos.baidu.com/video/pic/item/f703738da977391224eade15fb198618377ae2f2.jpg","新增数据", NSDate.date().description] , forKeys: ["video_img","video_title","video_subTitle"])
listVideos.insertObject(item, atIndex: 0)
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} // MARK: - Table view data source
//返回节的个数
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
//返回某个节中的行数
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return listVideos.count
}
//为表视图单元格提供数据,该方法是必须实现的方法
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier : String = "videoItem"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as JieTableViewCell
var row = indexPath.row
var rowDict : NSDictionary = listVideos.objectAtIndex(row) as NSDictionary
let url : String = rowDict.objectForKey("video_img") as String
let dataImg : NSData = NSData(contentsOfURL: NSURL(string : url))
cell.JieVideoImg.image = UIImage(data: dataImg)
cell.JieVideoTitle.text = rowDict.objectForKey("video_title") as? String
cell.JieVideoSubTitle.text = rowDict.objectForKey("video_subTitle") as? String
return cell } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { } // 支持单元格编辑功能
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
} // Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
listVideos.removeObjectAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
} // Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
if fromIndexPath != toIndexPath{
var object: AnyObject = listVideos.objectAtIndex(fromIndexPath.row)
listVideos.removeObjectAtIndex(fromIndexPath.row)
if toIndexPath.row > self.listVideos.count{
self.listVideos.addObject(object)
}else{
self.listVideos.insertObject(object, atIndex: toIndexPath.row)
}
}
} // Override to support conditional rearranging of the table view.
//在编辑状态。能够拖动设置item位置
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
} // MARK: - Navigation //给新进入的界面进行传值
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let object : NSDictionary = listVideos[indexPath.row] as NSDictionary
(segue.destinationViewController as JieDetailViewController).detailItem = object
}
}
} }
JieTableViewCell.swift
//
// JieTableViewCell.swift
// JieTableView
//
// Created by jiezhang on 14-10-5.
// Copyright (c) 2014年 jiezhang. All rights reserved.
// import UIKit class JieTableViewCell: UITableViewCell { @IBOutlet weak var JieVideoImg: UIImageView!
@IBOutlet weak var JieVideoTitle: UILabel!
@IBOutlet weak var JieVideoSubTitle: UILabel! override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
} override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated) } }
JieDetailViewController.swift
//
// JieDetailViewController.swift
// JieTableView
//
// Created by jiezhang on 14-10-5.
// Copyright (c) 2014年 jiezhang. All rights reserved.
// import UIKit class JieDetailViewController: UIViewController { @IBOutlet var big_video_img: UIImageView!
//接受传进来的值
var detailItem: NSDictionary? func configureView() {
if let detail : NSDictionary = self.detailItem {
self.title = detail.objectForKey("video_title") as? String
let url : String = detail.objectForKey("video_img") as String
let dataImg : NSData = NSData(contentsOfURL: NSURL(string : url))
self.big_video_img.image = UIImage(data: dataImg)
}
} override func viewDidLoad() {
super.viewDidLoad()
configureView()
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} // MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { } }
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvand6aGFuZ2ppZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvand6aGFuZ2ppZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvand6aGFuZ2ppZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
源代码地址:https://github.com/jwzhangjie/JieTableView
Swift基础--使用TableViewController自己定义列表的更多相关文章
- 第三篇、Swift基础学习
1.常量与变量 什么是常量和变量 在Swift中规定:在定义一个标识符时必须明确说明该标识符是一个常量还是变量 使用let来定义常量,定义之后不可以修改 使用var来定义变量,定义之后可以修改 变量的 ...
- Swift基础语法学习总结(转)
Swift基础语法学习总结 1.基础 1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift使用print和println打印,它的传参是一个泛型 ...
- Swift基础语法学习总结
Swift基础语法学习总结Swift高级语法学习总结Swift语法总结补充(一) 1.基础 1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift ...
- 第五篇:python基础之循环结构以及列表
python基础之循环结构以及列表 python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.pyth ...
- iOS Swift 模块练习/swift基础学习
SWIFT项目练习 SWIFT项目练习2 iOS Swift基础知识代码 推荐:Swift学习使用知识代码软件 0.swift中的宏定义(使用方法代替宏) 一.视图 +控件 1.UIImag ...
- Swift基础之如何使用iOS 9的Core Spotlight框架
本文由CocoaChina译者KingOfOnePiece(博客)翻译 作者:GABRIEL THEODOROPOULOS?校对:hyhSuper 原文:How To Use Core Spotlig ...
- Web开发——HTML基础(高级文本格式 列表/style)
文档资料参考: 参考:https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Advanced_text_fo ...
- HTML基础标签图片文本超链接列表表格介绍
1.HTML基础标签图片常见代码形式<img src="图片路径地址" alt="属性名" title="占位符">常见的图片格 ...
- SharePoint 2013 图文开发系列之代码定义列表
在SharePoint的开发中,用Visual Studio自定义列表是经常会用到的,因为很多时候,我们并不会手动创建列表,而手动创建列表在测试服务器和正式机之间同步字段,也很麻烦,所以我们经常用代码 ...
随机推荐
- Swift - 页控件(UIPageControl)的用法
使用页控件可以用来展示多个桌面.比如很多应用第一次登陆时,会在开始页面使用页控件来介绍功能,通过左右滑动来切换页. 通常我们使用UIPageControl和UIScrollView相互结合来实现多页切 ...
- Android面向HTTP协议发送get请求
/** * 採用get请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ pub ...
- 硬盘重装Ubuntu12.04的感受
好久没更blog了,最近这两天系统也出了问题,win7蓝屏,ubuntu进不去-.后来win7整好了,ubuntu依旧顽固.用惯了linux,就不想在转到win7下面了,估计是习惯了各种敲命令的感觉吧 ...
- maven使用.02.一些概念
在上一篇POST中,简要的介绍了一下maven的特点,优势,安装.并建立了一个简单地Hello world工程.这一篇POST中,将主要会介绍一下Maven的一些约定. pom.xml文件 Maven ...
- 深入浅出Node.js (1) - Node简介
1.1 Node的诞生历程 1.2 Node的命名与起源 1.2.1 为什么是JavaScript 1.2.2 为什么叫Node 1.3 Node给JavaScript带来的意义 1.4 Node的特 ...
- Storm集群中执行的各种组件及其并行
一.Storm中执行的组件 我们知道,Storm的强大之处就是能够非常easy地在集群中横向拓展它的计算能力,它会把整个运算过程切割成多个独立的tasks在集群中进行并行计算.在Storm中 ...
- 跨域GET、POST请求
跨域GET.POST请求的小结 重点:跨域POST大量数据: JQuery:$.ajax/$.getJSON支持jsonp格式的跨域,但是只支持GET方式,暂不支持POST: CORS:w3c关于跨域 ...
- 做一个牛XX的身份证号验证类(支持15位和18位)
原文:做一个牛XX的身份证号验证类(支持15位和18位) #region 是否合法的中国身份证号码 protected bool IsChineseID() { if (str.Length == 1 ...
- plsql导入一个目录下全部excel
import java.io.File; import java.util.ArrayList; import jxl.Sheet; import jxl.Workbook; import com.j ...
- 如何在cocos2d项目中enable ARC
如何在cocos2d项目中enable ARC 基本思想就是不支持ARC的代码用和支持ARC的分开,通过xcode中设置编译选项,让支持和不支持ARC的代码共存. cocos2d是ios app开发中 ...