首先建立一个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自己定义列表的更多相关文章

  1. 第三篇、Swift基础学习

    1.常量与变量 什么是常量和变量 在Swift中规定:在定义一个标识符时必须明确说明该标识符是一个常量还是变量 使用let来定义常量,定义之后不可以修改 使用var来定义变量,定义之后可以修改 变量的 ...

  2. Swift基础语法学习总结(转)

    Swift基础语法学习总结 1.基础  1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift使用print和println打印,它的传参是一个泛型 ...

  3. Swift基础语法学习总结

    Swift基础语法学习总结Swift高级语法学习总结Swift语法总结补充(一) 1.基础  1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift ...

  4. 第五篇:python基础之循环结构以及列表

    python基础之循环结构以及列表   python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.pyth ...

  5. iOS Swift 模块练习/swift基础学习

    SWIFT项目练习     SWIFT项目练习2 iOS Swift基础知识代码 推荐:Swift学习使用知识代码软件 0.swift中的宏定义(使用方法代替宏) 一.视图  +控件 1.UIImag ...

  6. Swift基础之如何使用iOS 9的Core Spotlight框架

    本文由CocoaChina译者KingOfOnePiece(博客)翻译 作者:GABRIEL THEODOROPOULOS?校对:hyhSuper 原文:How To Use Core Spotlig ...

  7. Web开发——HTML基础(高级文本格式 列表/style)

    文档资料参考: 参考:https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Advanced_text_fo ...

  8. HTML基础标签图片文本超链接列表表格介绍

    1.HTML基础标签图片常见代码形式<img src="图片路径地址" alt="属性名" title="占位符">常见的图片格 ...

  9. SharePoint 2013 图文开发系列之代码定义列表

    在SharePoint的开发中,用Visual Studio自定义列表是经常会用到的,因为很多时候,我们并不会手动创建列表,而手动创建列表在测试服务器和正式机之间同步字段,也很麻烦,所以我们经常用代码 ...

随机推荐

  1. Swift - 控制流/控制结构说明(if,switch,for,while)

    1,if语句 1 2 3 4 5 if count >=3 {     println("yes") }else{     println("no") } ...

  2. MySQL中同一时候存在创建和上次更新时间戳字段解决方法浅析

    在写这篇文章之前.明白我的MySQL版本号. mysql> SELECT VERSION(); +------------+ | VERSION() | +------------+ | 5.5 ...

  3. android binder机制之——(创建binder服务)

      Binder机制编程 前面的几篇文章具体介绍了android中binder机制的方方面面,相信你对binder机制已经有了较深刻的理解.俗话说得好"学以致用",以下我们就通过在 ...

  4. 创作gtk源码级vim帮助文档 tags

    创作gtk源码级vim帮助文档 tags 缘由 那只有看到源码了.在linux源码上有个网站 http://lxr.linux.no /+trees, 可以很方面的查出相应版本的代码实现,gtk没有. ...

  5. Disqus – About Disqus

    Disqus – About Disqus   Disqus is a free service that enables great online communities. As the web's ...

  6. 每天一个JavaScript实例-动态省份选择城市

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. [Android学习笔记]some tips

    集合合并去重: listA.removeAll(listB); listA.addAll(listB); android:singleLine="true"//单行显示 andro ...

  8. tasklet和workqueue的选择

    linux内核设计与实现page127中有个比較,内容比較多.概括一下就是1. tasklet不能休眠, 2. 不须要休眠tasklet效率更高 3.有休眠仅仅能workqueue (1)假设不须要休 ...

  9. nginx.conf 文中描述的配置文件

    ###############################nginx.conf 件里文说明 #user nobody; # user 主模块指令,指令nginx worker 执行用户和用户组(u ...

  10. 更改Oracle实例的字符集

    (1).数据库服务器字符集select * from nls_database_parameters 来源于props$,是表示数据库的字符集. (2).服务端字符集环境select * from n ...