Skip to content

Commit

Permalink
主题日报列表网络数据获取完成 但headerImageView暂无法更新
Browse files Browse the repository at this point in the history
  • Loading branch information
zpz1237 committed Oct 24, 2015
1 parent 0335dc3 commit 107c5f6
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 17 deletions.
21 changes: 16 additions & 5 deletions zhihuDaily 2.0/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

//获取主题列表
getThemesData()

//暂时使用首页contentStory里的数据
let themeStory: [ContentStoryModel] = contentStory + contentStory

themeContent = ThemeContentModel(stories: themeStory, background: "", name: "", editorsAvatars: ["avatar"])
return true
}

Expand Down Expand Up @@ -61,6 +56,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// MARK: - 数据相关
func getTodayData() {
Alamofire.request(.GET, "http://news-at.zhihu.com/api/4/news/latest").responseJSON { (_, _, resultData) -> Void in
guard resultData.error == nil else {
print("数据获取失败")
return
}
let data = JSON(resultData.value!)
//取到本日文章列表数据
let topStoryData = data["top_stories"]
Expand Down Expand Up @@ -88,6 +87,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let aDayBeforeURL = getCalenderString(NSDate().dateByAddingTimeInterval(28800).description)
let aDayBefore = getCalenderString(NSDate().dateByAddingTimeInterval(28800 - 86400).description)
Alamofire.request(.GET, "http://news.at.zhihu.com/api/4/news/before/" + aDayBeforeURL).responseJSON { (_, _, resultData) -> Void in
guard resultData.error == nil else {
print("数据获取失败")
return
}
let data = JSON(resultData.value!)

//取得日期Cell数据
Expand All @@ -108,6 +111,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let twoDayBeforeURL = aDayBefore
let twoDayBefore = self.getCalenderString(NSDate().dateByAddingTimeInterval(28800 - 2 * 86400).description)
Alamofire.request(.GET, "http://news.at.zhihu.com/api/4/news/before/" + twoDayBeforeURL).responseJSON { (_, _, resultData) -> Void in
guard resultData.error == nil else {
print("数据获取失败")
return
}
let data = JSON(resultData.value!)

//取得日期Cell数据
Expand All @@ -128,6 +135,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let threeDayBeforeURL = twoDayBefore
let threeDayBefore = self.getCalenderString(NSDate().dateByAddingTimeInterval(28800 - 3 * 86400).description)
Alamofire.request(.GET, "http://news.at.zhihu.com/api/4/news/before/" + threeDayBeforeURL).responseJSON { (_, _, resultData) -> Void in
guard resultData.error == nil else {
print("数据获取失败")
return
}
let data = JSON(resultData.value!)

//取得日期Cell数据
Expand Down
3 changes: 2 additions & 1 deletion zhihuDaily 2.0/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@
</view>
</navigationItem>
<connections>
<outlet property="navTitleLabel" destination="p15-qr-Sde" id="MZj-S2-0TV"/>
<outlet property="tableView" destination="Jfz-eR-Rlb" id="8Pl-Hk-Tld"/>
</connections>
</viewController>
Expand Down Expand Up @@ -676,6 +677,6 @@
<image name="switch" width="128" height="128"/>
</resources>
<inferredMetricsTieBreakers>
<segue reference="PgJ-Sp-ZXF"/>
<segue reference="IUO-ob-lY1"/>
</inferredMetricsTieBreakers>
</document>
10 changes: 10 additions & 0 deletions zhihuDaily 2.0/SideMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,14 @@ extension SideMenuViewController: UITableViewDataSource, UITableViewDelegate {
}
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//判定是否转场到主题日报文章列表
if let nav = segue.destinationViewController as? UINavigationController {
if let vc = nav.topViewController as? ThemeViewController {
let index = self.tableView.indexPathForSelectedRow!.row
vc.name = appCloud().themes[index - 1].name
vc.id = appCloud().themes[index - 1].id
}
}
}
}
4 changes: 1 addition & 3 deletions zhihuDaily 2.0/StoryModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ struct ThemeModel {
struct ThemeContentModel {
var stories: [ContentStoryModel]
var background: String
var name: String
var editorsAvatars: [String]
init (stories: [ContentStoryModel], background: String, name: String, editorsAvatars: [String]) {
init (stories: [ContentStoryModel], background: String, editorsAvatars: [String]) {
self.stories = stories
self.background = background
self.name = name
self.editorsAvatars = editorsAvatars
}
}
75 changes: 67 additions & 8 deletions zhihuDaily 2.0/ThemeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@
//

import UIKit
import Alamofire
import SwiftyJSON
import SDWebImage

class ThemeViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var navTitleLabel: UILabel!

var selectedNewsId = ""
var id = ""
var name = ""
var selectedIndex: [Int] = []
var navImageView: UIImageView!
var themeSubview: ParallaxHeaderView!

override func viewDidLoad() {
super.viewDidLoad()

//清空原数据
self.appCloud().themeContent = nil

//拿到新数据
refreshData()

//创建leftBarButtonItem
let leftButton = UIBarButtonItem(image: UIImage(named: "leftArrow"), style: .Plain, target: self.revealViewController(), action: "revealToggle:")
leftButton.tintColor = UIColor.whiteColor()
Expand All @@ -27,14 +40,14 @@ class ThemeViewController: UIViewController {
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

//生成并配置HeaderImageView
let navImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, 64))
navImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, 64))
navImageView.contentMode = UIViewContentMode.ScaleAspectFill
navImageView.clipsToBounds = true
let image = UIImage(named: "ThemeImage")!
navImageView.image = image

//将其添加到ParallaxView
let themeSubview = ParallaxHeaderView.parallaxThemeHeaderViewWithSubView(navImageView, forSize: CGSizeMake(self.view.frame.width, 64), andImage: image) as! ParallaxHeaderView
themeSubview = ParallaxHeaderView.parallaxThemeHeaderViewWithSubView(navImageView, forSize: CGSizeMake(self.view.frame.width, 64), andImage: navImageView.image) as! ParallaxHeaderView
themeSubview.delegate = self

//将ParallaxView设置为tableHeaderView,主View添加tableView
Expand All @@ -52,9 +65,50 @@ class ThemeViewController: UIViewController {
self.tableView.showsVerticalScrollIndicator = false
}

override func viewWillAppear(animated: Bool) {
func refreshData() {
//更改标题
navTitleLabel.text = name

//self.navigationController?.navigationBarHidden = false
//获取数据
Alamofire.request(.GET, "http://news-at.zhihu.com/api/4/theme/" + id).responseJSON { (_, _, dataResult) -> Void in
guard dataResult.error == nil else {
print("数据获取失败")
return
}
let data = JSON(dataResult.value!)

//取得Story
let storyData = data["stories"]
//暂时注入themeStory
var themeStory: [ContentStoryModel] = []
for i in 0 ..< storyData.count {
//判断是否含图
if storyData[i]["images"] != nil {
themeStory.append(ContentStoryModel(images: [storyData[i]["images"][0].string!], id: String(storyData[i]["id"]), title: storyData[i]["title"].string!))
} else {
//若不含图
themeStory.append(ContentStoryModel(images: [""], id: String(storyData[i]["id"]), title: storyData[i]["title"].string!))
}
}

//取得avatars
let avatarsData = data["editors"]
//暂时注入editorsAvatars
var editorsAvatars: [String] = []
for i in 0 ..< avatarsData.count {
editorsAvatars.append(avatarsData[i]["avatar"].string!)
}

//更新图片
// self.navImageView.sd_setImageWithURL(NSURL(string: data["background"].string!))
// self.themeSubview = ParallaxHeaderView.parallaxThemeHeaderViewWithSubView(self.navImageView, forSize: CGSizeMake(self.view.frame.width, 64), andImage: self.navImageView.image) as! ParallaxHeaderView

//注入themeContent
self.appCloud().themeContent = ThemeContentModel(stories: themeStory, background: data["background"].string!, editorsAvatars: editorsAvatars)

//刷新数据
self.tableView.reloadData()
}
}

//设置StatusBar颜色
Expand Down Expand Up @@ -82,6 +136,11 @@ extension ThemeViewController: UITableViewDelegate, UITableViewDataSource, Paral

//处理UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//如果还未获取到数据
if appCloud().themeContent == nil {
return 0
}
//如含有数据
return appCloud().themeContent!.stories.count + 1
}

Expand All @@ -93,7 +152,7 @@ extension ThemeViewController: UITableViewDelegate, UITableViewDataSource, Paral
avatar.contentMode = .ScaleAspectFill
avatar.layer.cornerRadius = 10
avatar.clipsToBounds = true
avatar.image = UIImage(named: editorsAvatar)
avatar.sd_setImageWithURL(NSURL(string: editorsAvatar))
cell.contentView.addSubview(avatar)
}
return cell
Expand All @@ -103,7 +162,7 @@ extension ThemeViewController: UITableViewDelegate, UITableViewDataSource, Paral
let tempContentStoryItem = appCloud().themeContent!.stories[indexPath.row - 1]

//保证图片一定存在,选择合适的Cell类型
guard let image = UIImage(named: tempContentStoryItem.images[0]) else {
guard tempContentStoryItem.images[0] != "" else {
let cell = tableView.dequeueReusableCellWithIdentifier("themeTextTableViewCell") as! ThemeTextTableViewCell
//验证是否已被点击过
if let _ = selectedIndex.indexOf(indexPath.row) {
Expand All @@ -124,7 +183,7 @@ extension ThemeViewController: UITableViewDelegate, UITableViewDataSource, Paral
cell.themeContentLabel.textColor = UIColor.blackColor()
}
cell.themeContentLabel.text = tempContentStoryItem.title
cell.themeContentImageView.image = image
cell.themeContentImageView.sd_setImageWithURL(NSURL(string: tempContentStoryItem.images[0]))

return cell
}
Expand Down

0 comments on commit 107c5f6

Please sign in to comment.