首先建立一個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.
// 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
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
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)
configureview()