Swift基础--使用TableViewController自定义列表

简介: <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 首先建立一个swift项目,把storyboard的内容删掉,添加一个Navigation Controller,然后

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

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


JieTableViewCell.swift

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.   
  9. import UIKit  
  10.   
  11. class JieTableViewCell: UITableViewCell {  
  12.   
  13.     @IBOutlet weak var JieVideoImg: UIImageView!  
  14.     @IBOutlet weak var JieVideoTitle: UILabel!  
  15.     @IBOutlet weak var JieVideoSubTitle: UILabel!  
  16.       
  17.     override func awakeFromNib() {  
  18.         super.awakeFromNib()  
  19.         // Initialization code  
  20.     }  
  21.   
  22.     override func setSelected(selected: Bool, animated: Bool) {  
  23.         super.setSelected(selected, animated: animated)  
  24.           
  25.      
  26.     }  
  27.   
  28. }  
JieDetailViewController.swift

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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.   
  9. import UIKit  
  10.   
  11. class JieDetailViewController: UIViewController {  
  12.       
  13.   
  14.     @IBOutlet var big_video_img: UIImageView!  
  15.     //接受传进来的值  
  16.     var detailItem: NSDictionary?  
  17.       
  18.       
  19.     func configureView() {  
  20.         if let detail : NSDictionary = self.detailItem {  
  21.             self.title = detail.objectForKey("video_title") as? String  
  22.             let url : String = detail.objectForKey("video_img") as String  
  23.             let dataImg : NSData = NSData(contentsOfURL: NSURL(string : url))  
  24.             self.big_video_img.image = UIImage(data: dataImg)  
  25.         }  
  26.     }  
  27.       
  28.     override func viewDidLoad() {  
  29.         super.viewDidLoad()  
  30.         configureView()  
  31.     }  
  32.   
  33.     override func didReceiveMemoryWarning() {  
  34.         super.didReceiveMemoryWarning()  
  35.         // Dispose of any resources that can be recreated.  
  36.     }  
  37.   
  38.     // MARK: - Navigation  
  39.     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {  
  40.          
  41.     }  
  42.   
  43. }  









目录
相关文章
|
Swift
Swift -banner滚动图自定义
Swift -banner滚动图自定义
183 0
Swift -banner滚动图自定义
|
Swift
Swift - 自定义tabbar的封装
Swift - 自定义tabbar的封装
286 0
Swift - 自定义tabbar的封装
|
Swift
Swift之自定义Button
Swift之自定义Button
195 0
Swift之自定义Button
|
Swift
Swift5.1—自定义运算符
Swift5.1—自定义运算符
116 0
|
Swift
swift微博第17天(UITableViewCell的自定义)
swift微博第17天(UITableViewCell的自定义)
212 0
|
Swift
swift微博第9天(自定义微博首页的菜单)
swift微博第9天(自定义微博首页的菜单)
154 0
swift微博第9天(自定义微博首页的菜单)
|
Swift
swift微博第8天(自定义标题按钮)
swift微博第8天(自定义标题按钮)
77 0
|
Swift iOS开发
UISlider自定义滑动条高度(swift)
UISlider滑动条的高度系统默认给我们设定了,没有办法进行改变。可是,我们的设计又是需要改变这个高度的,怎么办呢?这个时候,我们可以写一个UISlider的子类,重写四个方法就能实现了。
3462 0
SwiftUI直通车系列(5)—— 自定义绘制(二)
SwiftUI直通车系列(5)—— 自定义绘制
132 0
SwiftUI直通车系列(5)—— 自定义绘制(二)
|
编译器 Swift iOS开发
Swift解读专题三——基础运算符
Swift解读专题三——基础运算符
105 0