Skip to content

Instantly share code, notes, and snippets.

@tkc
Created August 20, 2016 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkc/d552431dc903621ce997bfa295046c70 to your computer and use it in GitHub Desktop.
Save tkc/d552431dc903621ce997bfa295046c70 to your computer and use it in GitHub Desktop.
class ChatViewController: SLKTextViewController {
var messages:[Message] = []
override class func tableViewStyleForCoder(decoder: NSCoder) -> UITableViewStyle {
return .Plain
}
override func viewDidLoad() {
super.viewDidLoad()
self.textView.placeholder = "Message";
if let tableView = self.tableView {
tableView.separatorStyle = .None
tableView.registerClass(TableViewCell.classForCoder(),forCellReuseIdentifier: "cell")
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 2
}
// NSNotification
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(ChatViewController.noticeReceived),
name: "notireceived", object: nil)
self.dummyMessage()
self.tableView!.sectionHeaderHeight = 300
}
func dummyMessage(){
for _ in 0..<10 {
let m = Message()
m.name = "Name"
m.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
self.messages.insert(m, atIndex: 0)
self.tableView?.reloadData()
}
self.tableView?.reloadData()
}
func noticeReceived() {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func addMessage(name: String,msg: String,right: Bool = true) {
let m = Message()
m.name = name
m.text = msg
self.messages.insert(m, atIndex: 0)
self.tableView?.reloadData()
}
override func didPressRightButton(sender: AnyObject?) {
addMessage("name",msg:self.textView.text)
self.textView.text = ""
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return messages.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell",forIndexPath: indexPath) as! TableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
let m = messages[indexPath.row]
cell.name.text = m.name
cell.comment.text = m.text
cell.comment.numberOfLines = 0
cell.transform = self.tableView!.transform
return cell
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
}
}
class Message {
var right: Bool = true
var text: String = ""
var name: String = ""
}
import UIKit
import SnapKit
import SlackTextViewController
class TableViewCell: UITableViewCell {
var comment:UILabel = UILabel()
var name :UILabel = UILabel()
var right_:Bool = true
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.autoresizingMask = autoresizingMask;
self.contentView.addSubview(name)
self.contentView.addSubview(comment)
name.snp_makeConstraints { (make) -> Void in
make.top.equalTo(self.contentView.snp_top).offset(20)
make.left.equalTo(self.contentView.snp_left).offset(20)
make.right.equalTo(self.contentView.snp_right).offset(-20)
}
comment.snp_makeConstraints { (make) -> Void in
make.top.equalTo(name.snp_bottom).offset(10)
make.left.equalTo(self.contentView.snp_left).offset(20)
make.bottom.equalTo(self.contentView.snp_bottom).offset(-10)
make.right.equalTo(self.contentView.snp_right).offset(-10)
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment