首先建立一个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

  1. //
  2. //  JieTableViewController.swift
  3. //  JieTableView
  4. //
  5. //  Created by jiezhang on 14-10-5.
  6. //  Copyright (c) 2014年 jiezhang. All rights reserved.
  7. //
  8. import UIKit
  9. class JieTableViewController: UITableViewController {
  10. var listVideos : NSMutableArray!
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. var bundle = NSBundle.mainBundle()
  14. let plistPath : String! = bundle.pathForResource("videos", ofType: "plist")
  15. listVideos = NSMutableArray(contentsOfFile: plistPath)
  16. // Uncomment the following line to preserve selection between presentations
  17. // self.clearsSelectionOnViewWillAppear = false
  18. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  19. //下面这段话是设置左边编辑,右边添加item
  20. self.navigationItem.leftBarButtonItem = self.editButtonItem()
  21. let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "insertNewObject:")
  22. self.navigationItem.rightBarButtonItem = addButton
  23. }
  24. func insertNewObject(sender: AnyObject) {
  25. 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"])
  26. listVideos.insertObject(item, atIndex: 0)
  27. let indexPath = NSIndexPath(forRow: 0, inSection: 0)
  28. self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
  29. }
  30. override func didReceiveMemoryWarning() {
  31. super.didReceiveMemoryWarning()
  32. // Dispose of any resources that can be recreated.
  33. }
  34. // MARK: - Table view data source
  35. //返回节的个数
  36. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  37. // #warning Potentially incomplete method implementation.
  38. // Return the number of sections.
  39. return 1
  40. }
  41. //返回某个节中的行数
  42. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  43. // #warning Incomplete method implementation.
  44. // Return the number of rows in the section.
  45. return listVideos.count
  46. }
  47. //为表视图单元格提供数据,该方法是必须实现的方法
  48. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  49. let cellIdentifier : String = "videoItem"
  50. let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as JieTableViewCell
  51. var row = indexPath.row
  52. var rowDict : NSDictionary = listVideos.objectAtIndex(row) as NSDictionary
  53. let url : String = rowDict.objectForKey("video_img") as String
  54. let dataImg : NSData = NSData(contentsOfURL: NSURL(string : url))
  55. cell.JieVideoImg.image = UIImage(data: dataImg)
  56. cell.JieVideoTitle.text = rowDict.objectForKey("video_title") as? String
  57. cell.JieVideoSubTitle.text = rowDict.objectForKey("video_subTitle") as? String
  58. return cell
  59. }
  60. override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  61. }
  62. // 支持单元格编辑功能
  63. override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
  64. // Return NO if you do not want the specified item to be editable.
  65. return true
  66. }
  67. // Override to support editing the table view.
  68. override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  69. if editingStyle == .Delete {
  70. // Delete the row from the data source
  71. listVideos.removeObjectAtIndex(indexPath.row)
  72. tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
  73. } else if editingStyle == .Insert {
  74. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  75. }
  76. }
  77. // Override to support rearranging the table view.
  78. override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
  79. if fromIndexPath != toIndexPath{
  80. var object: AnyObject = listVideos.objectAtIndex(fromIndexPath.row)
  81. listVideos.removeObjectAtIndex(fromIndexPath.row)
  82. if toIndexPath.row > self.listVideos.count{
  83. self.listVideos.addObject(object)
  84. }else{
  85. self.listVideos.insertObject(object, atIndex: toIndexPath.row)
  86. }
  87. }
  88. }
  89. // Override to support conditional rearranging of the table view.
  90. //在编辑状态,可以拖动设置item位置
  91. override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
  92. // Return NO if you do not want the item to be re-orderable.
  93. return true
  94. }
  95. // MARK: - Navigation
  96. //给新进入的界面进行传值
  97. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
  98. if segue.identifier == "showDetail" {
  99. if let indexPath = self.tableView.indexPathForSelectedRow() {
  100. let object : NSDictionary = listVideos[indexPath.row] as NSDictionary
  101. (segue.destinationViewController as JieDetailViewController).detailItem = object
  102. }
  103. }
  104. }
  105. }

JieTableViewCell.swift

  1. //
  2. //  JieTableViewCell.swift
  3. //  JieTableView
  4. //
  5. //  Created by jiezhang on 14-10-5.
  6. //  Copyright (c) 2014年 jiezhang. All rights reserved.
  7. //
  8. import UIKit
  9. class JieTableViewCell: UITableViewCell {
  10. @IBOutlet weak var JieVideoImg: UIImageView!
  11. @IBOutlet weak var JieVideoTitle: UILabel!
  12. @IBOutlet weak var JieVideoSubTitle: UILabel!
  13. override func awakeFromNib() {
  14. super.awakeFromNib()
  15. // Initialization code
  16. }
  17. override func setSelected(selected: Bool, animated: Bool) {
  18. super.setSelected(selected, animated: animated)
  19. }
  20. }

JieDetailViewController.swift

  1. //
  2. //  JieDetailViewController.swift
  3. //  JieTableView
  4. //
  5. //  Created by jiezhang on 14-10-5.
  6. //  Copyright (c) 2014年 jiezhang. All rights reserved.
  7. //
  8. import UIKit
  9. class JieDetailViewController: UIViewController {
  10. @IBOutlet var big_video_img: UIImageView!
  11. //接受传进来的值
  12. var detailItem: NSDictionary?
  13. func configureView() {
  14. if let detail : NSDictionary = self.detailItem {
  15. self.title = detail.objectForKey("video_title") as? String
  16. let url : String = detail.objectForKey("video_img") as String
  17. let dataImg : NSData = NSData(contentsOfURL: NSURL(string : url))
  18. self.big_video_img.image = UIImage(data: dataImg)
  19. }
  20. }
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. configureView()
  24. }
  25. override func didReceiveMemoryWarning() {
  26. super.didReceiveMemoryWarning()
  27. // Dispose of any resources that can be recreated.
  28. }
  29. // MARK: - Navigation
  30. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
  31. }
  32. }

源码地址:https://github.com/jwzhangjie/JieTableView

 

Swift基础--使用TableViewController自定义列表的更多相关文章

  1. Android零基础入门第39节:ListActivity和自定义列表项

    原文:Android零基础入门第39节:ListActivity和自定义列表项 相信通过前两期的学习,以及会开发最简单的一些列表界面了吧,那么本期接着来学习更多方法技巧. 一.使用ListActivi ...

  2. 2.html基础标签:无序+有序+自定义列表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Bootstrap <基础二十八>列表组

    列表组.列表组件用于以列表形式呈现复杂的和自定义的内容.创建一个基本的列表组的步骤如下: 向元素 <ul> 添加 class .list-group. 向 <li> 添加 cl ...

  4. WPF界面设计技巧(4)—自定义列表项样式

    原文:WPF界面设计技巧(4)-自定义列表项样式 有前面修改按钮样式的基础,我们可以尝试来定制一个即好看又好用的 ListBox ,今天先来讲“好看”部分. 打开 Microsoft Visual S ...

  5. jqgrid自定义列表开发=》实现高级查询

    标题已指出本文要说的三件事,首先是主角jqgrid,将应用在自定义列表中,重点介绍如何实现高级查询. 使用jqgrid实现自定义列表分为两大步骤,首先是要根据业务完成jqgrid的对象定义,即列表的描 ...

  6. swift基础:第六部分:类与对象

    http://reactnative.cn/docs/0.24/getting-started.html#content(react Native 开发文档) 互联网这个时代,你松懈一天,就会有很多很 ...

  7. swift基础:第二部分:函数和闭包

    今天本来想利用上午的时间本来打算将swift基础部分学习完的,不巧的是,后台来和我讨论用户评价的接口,讨论过后,商讨出一种可行的方案,十几分钟时间过去了,我拿到将接口介入到已经完成的页面中,完美,终于 ...

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

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

  9. SharePoint 2013开发入门探索(一)- 自定义列表

    在SharePoint 2013中创建自定义列表的方式有很多,在网站内容页面添加应用程序就可以创建(站点内容-〉 您的应用程序),也可以通过SharePoint Designer 2013创建,而本文 ...

随机推荐

  1. C/C++笔试准备(2)

    问题:编辑距离,是指将一个字符串变为另一个字符串,仅可以3种操作:修改一个字符,删除一个字符,插入一个字符.the变成that:删除e,插入a,插入t.20’ 实现编辑距离算法. 解算:利用动态规划的 ...

  2. Android控件属性大全[整理转载]

    控件属性: android属性 Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料, 第一类:属性值为true或falseandroid:layout_ ...

  3. ncsim仿真VHDL

    ncsim仿真VHDL 1.文件列表 ctrl.vhd design_io.vhd tb.vhd compile.nc simulate.nc ./shm/shmtb.tcl 2. Compile你的 ...

  4. 如何安装Windows Live Writer插件

    Windows Live Writer 是一个强大的离线博客编辑工具,通过它可以离线编辑内容丰富的博文.它不但支持微软的live space,还支持诸如Wordpress 这样的开源博客系统. Win ...

  5. silverlight .net后台 设置visifire控件图表样式 属性说明

    .net后台 代码: 如图 Chart chart = new MyCharts();  //图表            //chart.Watermark = false;  //没好使       ...

  6. OpenGL ES 2.0 雾

    在场景中使用雾不但可以提高真实感,特定的情况下还能优化性能.具体是指当物体离摄像机足够远时,雾就足够浓,此时只能看到雾而看不到物体,也就不必对物体着色进行详细计算,这样可以大大提高渲染效率. 雾有很多 ...

  7. 简单的LRU Cache设计与实现

    要求: 设计并实现一个LRU缓存的数据结构,支持get和set操作 get(key):若缓存中存在key,返回对应的value,否则返回-1 set(key,value):若缓存中存在key,替换其v ...

  8. 多线程中遇到ASSERT(pMap->LookupPermanent(hWndOrig) == NULL);怎么解决

    XP下用VC开发的程序,在一个主线程调用3   个线程,线程之间要共享数据,结果总出现wincore.cpp   line   980   ASSERT(pMap-> LookupPermane ...

  9. JS常用的方法

    1.时间戳转换 //时间戳(有Date和无Date的都可)转换为日期 “2016年5月30日 10:29:30 2016-05-20 09:11” function TimeConversion(ti ...

  10. js复制对象 和 节点类型和NodeList

    1. myList.cloneNode(true); 在参数为true的情况下,执行深复制,也就是复制节点及其整个子节点树,包括属性 2. myList.cloneNode(false); 在参数为f ...