Swift - 實現點擊cell動態修改高度 效果 源碼 https://github.com/YouXianMing/Swift-Animations ...
Swift - 實現點擊cell動態修改高度
效果
源碼
https://github.com/YouXianMing/Swift-Animations
// // TapCellAnimationController.swift // Swift-Animations // // Created by YouXianMing on 16/8/30. // Copyright © 2016年 YouXianMing. All rights reserved. // import UIKit class TapCellAnimationController: NormalTitleViewController, UITableViewDelegate, UITableViewDataSource { private var tableView : UITableView! private var datas : [CellDataAdapter]! override func setup() { super.setup() datas = [CellDataAdapter]() tableView = UITableView(frame: (contentView?.bounds)!) tableView.delegate = self tableView.dataSource = self tableView.separatorStyle = .None ShowTextCell.RegisterTo(tableView) contentView?.addSubview(tableView) func addText(string : String) { let model = ShowTextModel(string) datas.append(ShowTextCell.Adapter( data : model, cellHeight : ShowTextCell.HeightWithData(model), type : EShowTextCellType.NormalType.rawValue)) } GCDQueue.executeInMainQueue({ addText("AFNetworking is a delightful networking library for iOS and Mac OS X. It's built on top of the Foundation URL Loading System, extending the powerful high-level networking abstractions built into Cocoa. It has a modular architecture with well-designed, feature-rich APIs that are a joy to use. Perhaps the most important feature of all, however, is the amazing community of developers who use and contribute to AFNetworking every day. AFNetworking powers some of the most popular and critically-acclaimed apps on the iPhone, iPad, and Mac. Choose AFNetworking for your next project, or migrate over your existing projects—you'll be happy you did!") addText("黃色的樹林里分出兩條路,可惜我不能同時去涉足,我在那路口久久佇立,我向著一條路極目望去,直到它消失在叢林深處。但我卻選了另外一條路,它荒草萋萋,十分幽寂,顯得更誘人、更美麗,雖然在這兩條小路上,都很少留下旅人的足跡,雖然那天清晨落葉滿地,兩條路都未經腳印污染。呵,留下一條路等改日再見!但我知道路徑延綿無盡頭,恐怕我難以再回返。也許多少年後在某個地方,我將輕聲嘆息把往事回顧,一片樹林里分出兩條路,而我選了人跡更少的一條,從此決定了我一生的道路。") addText("★タクシー代がなかったので、家まで歩いて帰った。★もし事故が発生した場所、このレバーを引いて列車を止めてください。(丁)為了清楚地表示出一個短語或句節,其後須標逗號。如:★この薬を、夜寢る前に一度、朝起きてからもう一度、飲んでください。★私は、空を飛ぶ鳥のように、自由に生きて行きたいと思った。*****為了清楚地表示詞語與詞語間的關係,須標逗號。標註位置不同,有時會使句子的意思發生變化。如:★その人は大きな音にびっくりして、橫から飛び出した子供にぶつかった。★その人は、大きな音にびっくりして橫から飛び出した子供に、ぶつかった。") addText("Two roads diverged in a yellow wood, And sorry I could not travel both And be one traveler, long I stood And looked down one as far as I could To where it bent in the undergrowth; Then took the other, as just as fair, And having perhaps the better claim, Because it was grassy and wanted wear; Though as for that the passing there Had worn them really about the same, And both that morning equally lay In leaves no step had trodden black. Oh, I kept the first for another day! Yet knowing how way leads on to way, I doubted if I should ever come back. I shall be telling this with a sigh Somewhere ages and ages hence: Two roads diverged in a wood, and I- I took the one less traveled by, And that has made all the difference.") var indexPaths = [NSIndexPath]() for i in 0 ..< self.datas.count { indexPaths.append(NSIndexPath(forItem: i, inSection: 0)) } self.tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Fade) GCDQueue.executeInMainQueue({ let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forItem: 0, inSection: 0)) as! CustomCell cell.selectedEvent() }, afterDelaySeconds: 0.5) }, afterDelaySeconds: 0.5) } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { return tableView.dequeueAndLoadContentReusableCellFromAdapter(datas[indexPath.row], indexPath: indexPath, tableView: tableView) } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return datas.count } func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return datas[indexPath.row].cellHeight! } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.selectedEventWithIndexPath(indexPath) } }