各位好,好久不見璃诀,我又回來了弧可。
這篇文章是ios 開發(fā) 系列文章(hacking swift )的第一篇
首先我不會翻譯文章,會把項目的關(guān)鍵點說一下劣欢!
如果有誤請留言W厮小!凿将!謝謝
1.主要內(nèi)容
這篇文章你會用到學到 (變量校套、常量、tableview牧抵、imageView笛匙、文件相關(guān)侨把、故事版)的相關(guān)內(nèi)容!
2.運行后的效果
3.主要代碼
3.1 列出項目中所有的nssl開頭的圖片
//objects 是一個用來存放ssl圖片的數(shù)組妹孙。
title = "hacking swift 第一個項目"
//獲得NSFileManager對象 文件管理器
let fm = NSFileManager.defaultManager()
//path 為 只想app的一個資源目錄路徑
let path = NSBundle.mainBundle().resourcePath!
//items 為整個項目中的資源文件包括plist 圖片等文件
let items = try! fm.contentsOfDirectoryAtPath(path)
//將所有nssl開頭的文件(已經(jīng)約定好都是圖片)存放在obects數(shù)組秋柄。
for item in items {
if item.hasPrefix("nssl") {
objects.append(item)
}
}
3.2 NavigationController的RootViewController
RootViewController為一個tableview,如果不是UITableview類蠢正,則要繼承UITableViewController华匾。
UITableViewController的幾個數(shù)據(jù)源方法必須實現(xiàn):
...numberOfRowsInSection
...nimberOfSectionInTableView
...cellForRowAtIndexPath
將object中的數(shù)據(jù)展示在tableview中
3.3 push 到第二個視圖前的準備
在push 到第二個視圖之前要 做一些賦值的準備。
主要是重寫prepareForSegue方法
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
print(22222)
if segue.identifier == "imageView"{
let desController = segue.destinationViewController as! ImageViewController
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
desController.imageRoad = self.objects[indexPath.row]
}
}
4.顯示圖片
在prepareForSegue 中已將選中的圖片帶了過來所以直接在imageView中顯示就可以了机隙!
5.在圖片瀏覽頁面Navigationbar
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//即將顯示時 輕拍 可以 隱藏bar
navigationController?.hidesBarsOnTap = true
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
//返回時 設(shè)置成false蜘拉,防止主頁面輕拍時隱藏
navigationController?.hidesBarsOnTap = false
}
代碼在Hacking swift的官網(wǎng)上有。英文寫的更好有鹿,更完美旭旭,有什么問題盡管留言!