1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| // // FourthViewController.swift // Instagram // // Created by Ant on 3/31/16. // Copyright © 2016 Ant. All rights reserved. //
import UIKit
class FourthViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate {
@IBOutlet weak var tableViewLeft: UITableView! @IBOutlet weak var tableViewRight: UITableView! @IBOutlet weak var tabPanel: UIView! @IBOutlet weak var followBtn: UIButton! @IBOutlet weak var youBtn: UIButton! @IBOutlet weak var scrollView: UIScrollView! let scrollBar = UIView() var notifications: [Notification] = [] override func viewDidLoad() { super.viewDidLoad() //创建模拟数据 let notification1 = Notification(from: "CHENGKANG", fromAvatar: UIImage(named: "IMG_8040")!, to: "Deer", toAvatar: UIImage(named: "test")!, type: "like", images: [UIImage(named: "test")!, UIImage(named: "test")!, UIImage(named: "test")!, UIImage(named: "test")!, UIImage(named: "test")!, ], date: "3天前") let notification2 = Notification(from: "CHENGKANG", fromAvatar: UIImage(named: "IMG_8040")!, to: "Deer", toAvatar: UIImage(named: "test")!, type: "like", images: [UIImage(named: "test")!], date: "3天前") //添加模拟数据 self.notifications.append(notification1) self.notifications.append(notification2) //初始化要用到的参数 let WIDTH = self.view.frame.width let HEIGHT = self.view.frame.height - 60 - 30 - 49 //设置 tab 标签面板底部阴影 self.tabPanel.layer.shadowColor = COLOR_GREY.CGColor self.tabPanel.layer.shadowRadius = 0.5 self.tabPanel.layer.shadowOffset = CGSizeMake(0, 0.5) self.tabPanel.layer.shadowOpacity = 1 //添加 tab 标签面板底部蓝条 self.view.addSubview(self.scrollBar) self.scrollBar.backgroundColor = COLOR_LIGHT_BLUE self.scrollBar.frame = CGRectMake(0, 87, WIDTH / 2, 3) //初始化按钮颜色 self.followBtn.setTitleColor(COLOR_LIGHT_BLUE, forState: .Normal) //设置 scrollView delegate self.scrollView.delegate = self //设置 tableViewLeft delegate,并消除多余分割线 self.tableViewLeft.delegate = self self.tableViewLeft.dataSource = self self.tableViewLeft.tableFooterView = UIView() //设置 tableViewRight delegate,并消除多余分割线 self.tableViewRight.delegate = self self.tableViewRight.dataSource = self self.tableViewRight.tableFooterView = UIView()
//设置 scrollView contentSize self.scrollView.contentSize = CGSizeMake(WIDTH * 2, HEIGHT) //设置两个 tableView 大小位置 self.tableViewLeft.frame = CGRectMake(8, 0, WIDTH - 16, HEIGHT) self.tableViewRight.frame = CGRectMake(WIDTH + 8, 0, WIDTH - 16, HEIGHT) } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //可以通过判断当前 tableView 是否与某一个 TableView 相同来给定对应内容 // if tableView == self.tableViewRight { // if self.notifications[indexPath.row].images.count == 1 { // let cell = LikeWithPicCell() // cell.initCell(self.notifications[indexPath.row]) // return cell // } else { // let cell = LikeWithPicsCell() // cell.initCell(self.notifications[indexPath.row]) // return cell // } // } if self.notifications[indexPath.row].images.count == 1 { let cell = tableView.dequeueReusableCellWithIdentifier("LikeWithPicCell") as! LikeWithPicCell cell.initCell(self.notifications[indexPath.row]) return cell } else { let cell = tableView.dequeueReusableCellWithIdentifier("LikeWithPicsCell") as! LikeWithPicsCell cell.initCell(self.notifications[indexPath.row]) return cell } } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if tableView == self.tableViewRight { //此处 -1 是为了让展示内容有区分,因为用的相同数据 return self.notifications.count - 1 } return self.notifications.count } func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension } func scrollViewDidScroll(scrollView: UIScrollView) { //判断当前 scrollView 是我们项目中的 ScrollView 而非那两个 tableView if scrollView == self.scrollView { //改变 scrollBar x 坐标,达成同步滑动效果。 let offsetX = scrollView.contentOffset.x self.scrollBar.frame = CGRectMake(offsetX / 2, 87, self.view.frame.width / 2, 3) //对应修改 btn 文字颜色 if offsetX > self.view.frame.width / 2 { self.followBtn.setTitleColor(UIColor.blackColor(), forState: .Normal) self.youBtn.setTitleColor(COLOR_LIGHT_BLUE, forState: .Normal) } else { self.followBtn.setTitleColor(COLOR_LIGHT_BLUE, forState: .Normal) self.youBtn.setTitleColor(UIColor.blackColor(), forState: .Normal) } } } @IBAction func followBtnPressed(sender: UIButton) { //点击按钮时,通过动画移动到对应 tableView UIView.animateWithDuration(0.3, delay: 0, options: [.CurveEaseInOut], animations: { () -> Void in self.scrollView.contentOffset.x = 0 }, completion: nil) } @IBAction func youBtnPressed(sender: UIButton) { //点击按钮时,通过动画移动到对应 tableView UIView.animateWithDuration(0.3, delay: 0, options: [.CurveEaseInOut], animations: { () -> Void in self.scrollView.contentOffset.x = self.view.frame.width }, completion: nil) }
}
|