Swift 實現(xiàn)QQ好友列表功能

整理了一下實現(xiàn)QQ好友列表的功能稠茂,數(shù)據(jù)也是自己手動拼接模擬的
QQFriend 存儲好友數(shù)據(jù)信息


class QQFriend: NSObject {

var name: String?

var intro: String?

init(dic: NSDictionary) {

super.init()

self.setValuesForKeys(dic as! [String : AnyObject])

}

}

QQFriendGroup 存儲好友分組數(shù)據(jù)


class QQFriendGroup: NSObject {

var name: String?

var friends: NSArray?

var isOpen: Bool? = false

init(withDic dic: NSDictionary) {

super.init()

self.setValuesForKeys(dic as! [String : AnyObject])

let arrayFriend: NSMutableArray = NSMutableArray()

for friendDic in self.friends! {

let friend : QQFriend = QQFriend.init(dic: friendDic as! NSDictionary)

arrayFriend.add(friend)

}

self.friends = arrayFriend

}

}

QQHeaderView 分組View


//協(xié)議

protocol QQHeaderViewDelegate: NSObjectProtocol {

func headerViewDidClickedNameLab(headerView:QQHeaderView)

}

class QQHeaderView: UITableViewHeaderFooterView {

weak var delegate:QQHeaderViewDelegate?

var nameLabel: UILabel? = {

let nameLabel = UILabel()

return nameLabel

}()

var arrow: UIImageView? = {

let arrow = UIImageView.init(image: UIImage(named: "orderDown"))

return arrow

}()

var group: QQFriendGroup? {

didSet {

self.nameLabel?.text = group?.name

didMoveToSuperview()

}

}

var clickNameBlock: ((Void)->())? = nil

var dividerView : UIView! = nil

class func headerViewWithTableView(tableview: UITableView) -> QQHeaderView {

let headerID = "myHeader"

var header = tableview.dequeueReusableHeaderFooterView(withIdentifier: headerID)

if header == nil {

header = QQHeaderView.init(reuseIdentifier: headerID)

}

return header as! QQHeaderView

}

override init(reuseIdentifier: String?) {

super.init(reuseIdentifier: reuseIdentifier)

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(QQHeaderView.nameClick))

self.nameLabel?.isUserInteractionEnabled = true

self.arrow?.isUserInteractionEnabled = true

self.contentView.addGestureRecognizer(tap)

self.contentView.addSubview(nameLabel!)

self.contentView.addSubview(arrow!)

let divideView = UIFactory.create_AView()

divideView.backgroundColor = UIColor.hrgb("cccccc")

self.contentView.addSubview(divideView)

self.dividerView = divideView

}

required init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

//點擊展開或收起列表

func nameClick() {

self.group?.isOpen = !(self.group?.isOpen)!

//        if ((self.delegate?.responds(to: Selector(("headerViewDidClickedNameLab:")))) != nil) {

//            self.delegate?.headerViewDidClickedNameLab(headerView: self)

//        }

if self.clickNameBlock != nil {

self.clickNameBlock!()

}

}

override func didMoveToSuperview() {

super.didMoveToSuperview()

if self.group?.isOpen == true {

self.arrow?.transform = CGAffineTransform.init(rotationAngle: .pi)

}else {

self.arrow?.transform = CGAffineTransform.init(rotationAngle: 0)

}

}

override func layoutSubviews() {

super.layoutSubviews()

self.nameLabel?.snp.makeConstraints({ (make) in

make.left.equalTo(15)

make.top.equalTo(0)

make.bottom.equalTo(0)

make.right.equalTo(-30)

})

self.arrow?.snp.makeConstraints({ (make) in

make.right.equalTo(-10)

make.width.height.equalTo(14)

make.centerY.equalTo(self.height * 0.5)

})

self.dividerView.snp.makeConstraints { (make) in

make.height.equalTo(0.5)

make.left.right.bottom.equalTo(0)

}

}

}

ImitationQQ 控制器 實現(xiàn)功能


extension ImitationQQ {

func numberOfSections(in tableView: UITableView) -> Int {

return (self.groups?.count)!

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

let group: QQFriendGroup = self.groups![section] as! QQFriendGroup

return group.isOpen == true ? group.friends!.count : 0

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "default", for: indexPath)

cell.textLabel?.text = "\(indexPath.section)" + "\(indexPath.row)"

cell.selectionStyle = .none

return cell

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

return 40

}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

return 0.01

}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let header: QQHeaderView = QQHeaderView.headerViewWithTableView(tableview: tableView)

header.delegate = self

header.clickNameBlock = {

self.tabView.reloadData()

}

header.group = self.groups![section] as? QQFriendGroup

return header

}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

return 44

}

}

extension ImitationQQ {

func headerViewDidClickedNameLab(headerView: QQHeaderView) {

self.tabView.reloadData()

}

}

class ImitationQQ: UIViewController, UITableViewDataSource, UITableViewDelegate, QQHeaderViewDelegate {

var tabView: UITableView! = nil

var dividerView : UIView! = nil

lazy var groups : NSArray? = {

var arr : NSMutableArray = NSMutableArray()

for i in 0..<3 {

let group: QQFriendGroup = QQFriendGroup.init(withDic: ["name":"\(i)" + "組", "friends":[["name":"00","intro":"挺好的"],["name":"01","intro":"挺好好的"],["name":"02","intro":"挺好好好的"],["name":"03","intro":"挺好好好好的"]]] as NSDictionary)

arr.add(group)

}

return arr

}()

override func viewDidLoad() {

super.viewDidLoad()

self.view.backgroundColor = UIColor.hrgb("f4f4f4")

self.title = "好友列表"

let tabView = UIFactory.create_ATableView(frame: CGRect.init(x: 0, y: 0.5, width: Screen_Width, height: kScreenHeight-64.5), delegate: self, dataSource: self, superView: self.view, type: .plain)

tabView.backgroundColor = UIColor.hrgb("f4f4f4")

tabView.register(UITableViewCell.self, forCellReuseIdentifier: "default")

self.tabView = tabView

let divideView = UIFactory.create_AView()

divideView.frame = CGRect(x: 0, y: 0, width: Screen_Width, height: 0.5)

divideView.backgroundColor = UIColor.hrgb("cccccc")

self.view.addSubview(divideView)

self.dividerView = divideView

}

}

不太會排版,這么貼代碼好low料睛,下次改進(jìn)一下丐箩。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末摇邦,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子屎勘,更是在濱河造成了極大的恐慌施籍,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,946評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件概漱,死亡現(xiàn)場離奇詭異丑慎,居然都是意外死亡,警方通過查閱死者的電腦和手機瓤摧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評論 3 399
  • 文/潘曉璐 我一進(jìn)店門竿裂,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人照弥,你說我怎么就攤上這事腻异。” “怎么了这揣?”我有些...
    開封第一講書人閱讀 169,716評論 0 364
  • 文/不壞的土叔 我叫張陵悔常,是天一觀的道長。 經(jīng)常有香客問我曾沈,道長这嚣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,222評論 1 300
  • 正文 為了忘掉前任塞俱,我火速辦了婚禮姐帚,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘障涯。我一直安慰自己罐旗,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,223評論 6 398
  • 文/花漫 我一把揭開白布唯蝶。 她就那樣靜靜地躺著九秀,像睡著了一般。 火紅的嫁衣襯著肌膚如雪粘我。 梳的紋絲不亂的頭發(fā)上鼓蜒,一...
    開封第一講書人閱讀 52,807評論 1 314
  • 那天,我揣著相機與錄音征字,去河邊找鬼都弹。 笑死,一個胖子當(dāng)著我的面吹牛匙姜,可吹牛的內(nèi)容都是我干的畅厢。 我是一名探鬼主播,決...
    沈念sama閱讀 41,235評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼氮昧,長吁一口氣:“原來是場噩夢啊……” “哼框杜!你這毒婦竟也來了浦楣?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,189評論 0 277
  • 序言:老撾萬榮一對情侶失蹤咪辱,失蹤者是張志新(化名)和其女友劉穎振劳,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體油狂,經(jīng)...
    沈念sama閱讀 46,712評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡澎迎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,775評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了选调。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,926評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡灵份,死狀恐怖仁堪,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情填渠,我是刑警寧澤弦聂,帶...
    沈念sama閱讀 36,580評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站氛什,受9級特大地震影響莺葫,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜枪眉,卻給世界環(huán)境...
    茶點故事閱讀 42,259評論 3 336
  • 文/蒙蒙 一捺檬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧贸铜,春花似錦堡纬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至棍鳖,卻和暖如春炮叶,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背渡处。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評論 1 274
  • 我被黑心中介騙來泰國打工镜悉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人骂蓖。 一個月前我還...
    沈念sama閱讀 49,368評論 3 379
  • 正文 我出身青樓积瞒,卻偏偏與公主長得像,于是被迫代替她去往敵國和親登下。 傳聞我的和親對象是個殘疾皇子茫孔,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,930評論 2 361

推薦閱讀更多精彩內(nèi)容