// ExploreTableViewController.swift // // librx // // Created by Yunlong Zheng on 15/10/25. // Copyright © 2015年 librx. All rights reserved. // import UIKit class ExploreTableViewController: UITableViewController { private var postsLoader = PostsLoader(.Default) private var explores:[ExploreSource] = [] private var targetPost:Post? private var targetUserId:Int? private var targetUserName:String? var dataUrl:String? override func viewDidLoad() { super.viewDidLoad() self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() } override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) self.navigationController?.navigationBarHidden = false self.navigationController?.setNavigationBarHidden(false, animated: true) refreshContent() self.tableView.reloadData() } override func prefersStatusBarHidden() -> Bool { return false } func refreshContent() { loadPostsFromUrl(self.dataUrl!) self.tableView.reloadData() } func loadPostsFromUrl(dataUrl: String) { postsLoader.loadExploreSourcesFromUrl(url: dataUrl) { [weak self](sources) -> () in if let selfView = self { selfView.explores = sources selfView.tableView.reloadData() } } } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return explores.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { if (indexPath.row == self.explores.count) { self.loadMoreData() return cellForLoadMore(cellForRowAtIndexPath: indexPath) }else{ return cellForRowsInSection(cellForRowAtIndexPath: indexPath) } } override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { if let indexPath = self.tableView.indexPathForSelectedRow{ let source = self.explores[indexPath.row] if source.source_type == "post" { targetPost = Post(id: source.id, title: source.title) performSegueWithIdentifier("showPostDetailsSegue", sender: self) }else if source.source_type == "user" { targetUserId = source.id targetUserName = source.title performSegueWithIdentifier("showUserDetailsSegue", sender: self) } } } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { if self.explores.count==0 { let messageLabel: UILabel = UILabel.init(frame: CGRect(x: 10, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height)) messageLabel.text = "当前没有可显示的内容,请下拉刷新" messageLabel.font.fontWithSize(10.0) messageLabel.textColor = UIColor.lightGrayColor() messageLabel.numberOfLines = 0 messageLabel.textAlignment = NSTextAlignment.Center messageLabel.sizeToFit() self.tableView.backgroundView = messageLabel self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None; }else { self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine; self.tableView.backgroundView = nil return 1; } return 0; } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "showPostDetailsSegue" { let destinationController = segue.destinationViewController as! WebViewController destinationController.post = targetPost } else if segue.identifier == "showUserDetailsSegue" { let destinationController = segue.destinationViewController as! ProfileViewController destinationController.userId = targetUserId destinationController.isMe = false destinationController.forceHiddenBackButton = true destinationController.title = "" } } func loadMoreData() { print("load more data....") } func cellForLoadMore(cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = UITableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "moretag") let label = UILabel.init(frame: CGRectMake(0, 10, self.view.frame.width, 20)) label.text = "加载更多" label.backgroundColor = UIColor.clearColor() label.textAlignment = .Center label.font = label.font.fontWithSize(14.0) label.tintColor = UIColor.blueColor() cell.contentView.addSubview(label) let activityView = UIActivityIndicatorView.init(frame: CGRectMake(0, 30 ,self.view.frame.width,30.0)) activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray activityView.hidesWhenStopped = false activityView.startAnimating() cell.contentView.addSubview(activityView) return cell } func cellForRowsInSection(cellForRowAtIndexPath indexPath: NSIndexPath) -> ExploreTableCell { let source = explores[indexPath.row] as ExploreSource let cellIdentifier = "Cell" let cell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:indexPath) as! ExploreTableCell let thumbnailUrl = NSURL(string: source.img) cell.sourceTitle.text = source.title cell.sourceTitle.lineBreakMode = NSLineBreakMode.ByWordWrapping cell.sourceTitle.numberOfLines = 2 cell.sourceImg.sd_setImageWithURL(thumbnailUrl, placeholderImage: UIImage(named: "default")) cell.sourceImg.layer.cornerRadius = cell.sourceImg.frame.size.width / 2 cell.sourceImg.clipsToBounds = true cell.sourceImg.layer.borderWidth = 1 cell.sourceImg.layer.borderColor = UIColor.whiteColor().CGColor cell.descriptionLabel.text = source.description return cell } }