首先建立一个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. ThinkPhp学习06

    原文:ThinkPhp学习06 一.简单学习修改用户信息模块 1.编写UserAction.class.php <?php class UserAction extends Action{ pu ...

  2. 大白菜U盘启动制作工具装机维护版V5.0–大白菜U盘下载中心

    大白菜U盘启动制作工具装机维护版V5.0–大白菜U盘下载中心   大白菜U盘启动制作工具装机维护版V5.0

  3. 模板的Traits

    Traits含义就是特性,应用Trait模板参数,使得我们的程序既保持灵活性,同时减少类型参数的数量.能够使得我们对函数进行更加细粒度的控制. #ifndef TRAIT_H_ #define TRA ...

  4. Twenty Newsgroups Classification实例任务之TrainNaiveBayesJob(一)

    接着上篇blog,继续看log里面的信息如下: + echo 'Training Naive Bayes model' Training Naive Bayes model + ./bin/mahou ...

  5. [Android学习笔记]jackson库的使用

    Jackson库一般用于序列化和反序列化操作,通常会涉及到的操作是: 1. Java Object -> Json String 2. Java Object -> Xml String ...

  6. java基础---->摘要算法的介绍 (转)

    数据摘要算法是密码学算法中非常重要的一个分支,它通过对所有数据提取指纹信息以实现数据签名.数据完整性校验等功能,由于其不可逆性,有时候会被用做敏感信息的加密.数据摘要算法也被称为哈希(Hash)算法. ...

  7. UVa 10286 - Trouble with a Pentagon

    题目:如图在正五边形中画一个正方形,一直正五边形边长,求正方形边长. 分析:计算几何,解析几何.求出边长的比例关系带入求解就可以. 设正五边形边长为1,在顶点建立直角坐标系,则左上角坐标为:B(-co ...

  8. 黑马程序猿————OC在Foundation框架结构和字符串

    ------<a href="http://www.itheima.com" target="blank">Java火车.Android火车.iOS ...

  9. poj-2195-Going Home最小费用最大流

    重新切一遍最小费用最大流~~~ 这到题目的数据范围有问题,尽量开大就好了~~ #include<stdio.h> #include<iostream> #include< ...

  10. Android在子线程中更新UI(二)

    MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...