标签:
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, target: self, 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, atIndex: 0) 35 let indexPath = NSIndexPath(forRow: 0, inSection: 0) 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 }
swift tableviewcontroller自定义列表
标签:
原文地址:http://www.cnblogs.com/lovecc/p/4386085.html