1.通过对知乎日报头条的url进行Json解析,并使用代理将网络请求之后的数据传回到ViewController当中。在这里利用了Swift中使用很多Alamofire网络库,可以直接在Cocoapod中进行安装下载。在该工程项目下建一个Podfile文件,里面添加如下所示命令。
platform :ios,'8.0'
use_frameworks!
pod 'Alamofire', '~>3.0'
func alamofireRequest(url: String){ var model: NewsCellModel? Alamofire.request(.GET, url, parameters: ["foo": "bar"]) .responseJSON { response in if let JSON = response.result.value { if let json = JSON.objectForKey("stories"){ for var i = 0; i<json.count; i = i + 1{ let str1 = json[i].objectForKey("id")as! Int //注意images格式,字符串数组 let str2 = json[i].objectForKey("images")![0]as! String let str3 = json[i].objectForKey("title")as! String // print(str1) // print(str2) // print(str3) model = NewsCellModel(id: str1, newsImageName: str2, newsTitle: str3) self.item.append(model!) } } if let json = JSON.objectForKey("top_stories"){ for var i = 0; i<json.count; i = i + 1{ let str1 = json[i].objectForKey("id")as! Int //注意images格式,字符串数组 let str2 = json[i].objectForKey("image")as! String let str3 = json[i].objectForKey("title")as! String // print(str1) // print(str2) // print(str3) model = NewsCellModel(id: str1, newsImageName: str2, newsTitle: str3) self.item.append(model!) } } //调用代理方法 self.delegate?.transforValue(self.item) } } }
2.添加头条新闻
在TableView视图上面有一个TableHeader视图,可以将头条新闻展示在TableHeader位置。
/** Description:加载TableHeader视图 */ func loadHeaderView(){ headerView = UIView(frame: CGRectMake(0,0,width,height/3)) self.tableView.tableHeaderView = headerView scrollView = UIScrollView(frame: CGRectMake(0,0,width,height/3)) // scrollView.backgroundColor = UIColor.redColor() //一定要加上scrollView的代理,不然不能进行回调 scrollView.delegate = self scrollView.contentSize = CGSize(width: width*7, height: height/3) //按页移动 scrollView.pagingEnabled = true scrollView.bounces = true //不显示水平移动的标记 scrollView.showsHorizontalScrollIndicator = false headerView.addSubview(scrollView) //button的width要注意width*7 button = UIButton(frame: CGRectMake(0,0,width*7,height/3)) button.addTarget(self, action: "jumpNext", forControlEvents: UIControlEvents.TouchUpInside) scrollView.addSubview(button) pageControl = UIPageControl(frame: CGRectMake(width/3,height/3.5,width/2,23)) pageControl.numberOfPages = 5 pageControl.pageIndicatorTintColor = UIColor.yellowColor() pageControl.currentPageIndicatorTintColor = UIColor.redColor() headerView.addSubview(pageControl) for i in 1...self.items2.count{ let model = self.items2[i - 1] let imageView = UIImageView(image: UIImage(data: NSData(contentsOfURL: NSURL(string: model.newsImageName)!)!)) imageView.frame = CGRectMake(width*CGFloat(i), 0, width, height/3) scrollView.addSubview(imageView) let label = UILabel(frame: CGRectMake(100 + CGFloat(i) * width,100,width/2,height/9)) label.text = model.newsTitle label.textColor = UIColor.whiteColor() label.shadowColor = UIColor.blackColor() label.numberOfLines = 3 scrollView.addSubview(label) } //添加第一页和最后一页 let imageView1 = UIImageView(image: UIImage(data: NSData(contentsOfURL: NSURL(string: self.items2.last!.newsImageName)!)!)) let label1 = UILabel(frame: CGRectMake(100 + CGFloat(0) * width,100,width/2,height/9)) label1.text = self.items2[4].newsTitle label1.textColor = UIColor.whiteColor() label1.shadowColor = UIColor.blackColor() label1.numberOfLines = 3 imageView1.frame = CGRectMake(width*CGFloat(0), 0, width, height/3) let imageView2 = UIImageView(image: UIImage(data: NSData(contentsOfURL: NSURL(string: self.items2.first!.newsImageName)!)!)) let label2 = UILabel(frame: CGRectMake(100 + CGFloat(6) * width,100,width/2,height/9)) label2.text = self.items2[0].newsTitle label2.textColor = UIColor.whiteColor() label2.shadowColor = UIColor.blackColor() label2.numberOfLines = 3 imageView2.frame = CGRectMake(width*CGFloat(6), 0, width, height/3) scrollView.addSubview(imageView1) scrollView.addSubview(label1) scrollView.addSubview(imageView2) scrollView.addSubview(label2) }
3.具体代码请见我的Github ,欢迎star~