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!) {
- }
- }
源码地址:https://github.com/jwzhangjie/JieTableView
Swift基础--使用TableViewController自定义列表的更多相关文章
- Android零基础入门第39节:ListActivity和自定义列表项
原文:Android零基础入门第39节:ListActivity和自定义列表项 相信通过前两期的学习,以及会开发最简单的一些列表界面了吧,那么本期接着来学习更多方法技巧. 一.使用ListActivi ...
- 2.html基础标签:无序+有序+自定义列表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Bootstrap <基础二十八>列表组
列表组.列表组件用于以列表形式呈现复杂的和自定义的内容.创建一个基本的列表组的步骤如下: 向元素 <ul> 添加 class .list-group. 向 <li> 添加 cl ...
- WPF界面设计技巧(4)—自定义列表项样式
原文:WPF界面设计技巧(4)-自定义列表项样式 有前面修改按钮样式的基础,我们可以尝试来定制一个即好看又好用的 ListBox ,今天先来讲“好看”部分. 打开 Microsoft Visual S ...
- jqgrid自定义列表开发=》实现高级查询
标题已指出本文要说的三件事,首先是主角jqgrid,将应用在自定义列表中,重点介绍如何实现高级查询. 使用jqgrid实现自定义列表分为两大步骤,首先是要根据业务完成jqgrid的对象定义,即列表的描 ...
- swift基础:第六部分:类与对象
http://reactnative.cn/docs/0.24/getting-started.html#content(react Native 开发文档) 互联网这个时代,你松懈一天,就会有很多很 ...
- swift基础:第二部分:函数和闭包
今天本来想利用上午的时间本来打算将swift基础部分学习完的,不巧的是,后台来和我讨论用户评价的接口,讨论过后,商讨出一种可行的方案,十几分钟时间过去了,我拿到将接口介入到已经完成的页面中,完美,终于 ...
- Swift基础语法学习总结(转)
Swift基础语法学习总结 1.基础 1.1) swift还是使用// 和/* */ 来注释,并且/* */允许多行注释. 1.2) swift使用print和println打印,它的传参是一个泛型 ...
- SharePoint 2013开发入门探索(一)- 自定义列表
在SharePoint 2013中创建自定义列表的方式有很多,在网站内容页面添加应用程序就可以创建(站点内容-〉 您的应用程序),也可以通过SharePoint Designer 2013创建,而本文 ...
随机推荐
- EasyInvoice 使用教程 - (1) 认识 EI
原视频下载地址:EI 主界面介绍 1. 主界面截图 2. 基础资料界面截图 3. 管理员 界面截图
- VB6.0连接MySQL数据库
VB6.0连接MySQL数据库
- WebApi2官网学习记录--HttpClient Message Handlers
在客户端,HttpClient使用message handle处理request.默认的handler是HttpClientHandler,用来发送请求和获取response从服务端.可以在clien ...
- Asp.Net EF Code First 简单入门
今天在上班期间花了点时间学习了一下微软的EntityFramework Code First技术,这篇文章只是简单的入门,不多废话,下面直入主题. 一.首先添加一个解决方案,接着添加一个web网站,D ...
- SqlDbHelper备份,做项目时方便应用(目前不太全,把自己项目中的逐渐转移过来)
****************************************** 这是官网新闻左侧类别那部分用到的 **************************************** ...
- Oracle 获取表结构信息
通过Oracle中的user_tab_cols, user_col_comments, user_constraints, user_cons_columns表联合查询. user_tab_cols用 ...
- RMAN之进入RMAN(转)
通过RMAN的方式不论要备份还是要恢复,都必须先启动实例并加载数据库. SQL> shutdown immediate数据库已经关闭.已经卸载数据库.ORACLE 例程已经关闭. C:\User ...
- 此项目的默认Web访问模式设置为文件共享, 但是无法从路径(此为转贴)
故障现象: 当你打开ASP.NET Web项目时,如果出现这样的错误提示:提示窗口标题: Web访问失败提示内容: 此项目的默认Web访问模式设置为文件共享, 但是无法从路径“...”打开“...”处 ...
- java 面对对象(抽象 继承 接口 多态)
什么是继承? 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继承那个类即可. 多个类可以称为子类,单独这个类称为父类.超类或者基类. 子类可以直接 ...
- encodeURI后台乱码(解决)
window.location.href = xxxx?a=encodeURI(encodeURI(name)) ; name是中文,页面部分需要编码两次 name = java.net.URLDec ...