Skip to content

Commit 35d7df9

Browse files
author
Ignacio Romero Zurbuchen
committed
Overrides tableView in the Swift sample project, to avoiding unwrapping too many times.
1 parent e5be026 commit 35d7df9

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

Examples/Messenger-Swift/MessageViewController.swift

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class MessageViewController: SLKTextViewController {
2323

2424
var editingMessage = Message()
2525

26+
override var tableView: UITableView {
27+
get {
28+
return super.tableView!
29+
}
30+
}
31+
2632

2733
// MARK: - Initialisation
2834

@@ -33,7 +39,7 @@ class MessageViewController: SLKTextViewController {
3339

3440
func commonInit() {
3541

36-
NSNotificationCenter.defaultCenter().addObserver(self.tableView!, selector: #selector(UITableView.reloadData), name: UIContentSizeCategoryDidChangeNotification, object: nil)
42+
NSNotificationCenter.defaultCenter().addObserver(self.tableView, selector: #selector(UITableView.reloadData), name: UIContentSizeCategoryDidChangeNotification, object: nil)
3743
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MessageViewController.textInputbarDidMove(_:)), name: SLKTextInputbarDidMoveNotification, object: nil)
3844

3945
// Register a SLKTextView subclass, if you need any special appearance and/or behavior customisation.
@@ -80,10 +86,8 @@ class MessageViewController: SLKTextViewController {
8086
self.typingIndicatorView!.canResignByTouch = true
8187
}
8288

83-
if let tableView = self.tableView {
84-
tableView.separatorStyle = .None
85-
tableView.registerClass(MessageTableViewCell.classForCoder(), forCellReuseIdentifier: MessengerCellIdentifier)
86-
}
89+
self.tableView.separatorStyle = .None
90+
self.tableView.registerClass(MessageTableViewCell.classForCoder(), forCellReuseIdentifier: MessengerCellIdentifier)
8791

8892
self.autoCompletionView.registerClass(MessageTableViewCell.classForCoder(), forCellReuseIdentifier: AutoCompletionCellIdentifier)
8993
self.registerPrefixesForAutoCompletion(["@", "#", ":", "+:", "/"])
@@ -236,7 +240,7 @@ extension MessageViewController {
236240
self.editingMessage = self.messages[cell.indexPath.row]
237241
self.editText(self.editingMessage.text)
238242

239-
self.tableView!.scrollToRowAtIndexPath(cell.indexPath, atScrollPosition: .Bottom, animated: true)
243+
self.tableView.scrollToRowAtIndexPath(cell.indexPath, atScrollPosition: .Bottom, animated: true)
240244
}
241245

242246
func editRandomMessage(sender: AnyObject) {
@@ -256,14 +260,14 @@ extension MessageViewController {
256260
return
257261
}
258262

259-
let lastSectionIndex = self.tableView!.numberOfSections-1
260-
let lastRowIndex = self.tableView!.numberOfRowsInSection(lastSectionIndex)-1
263+
let lastSectionIndex = self.tableView.numberOfSections-1
264+
let lastRowIndex = self.tableView.numberOfRowsInSection(lastSectionIndex)-1
261265

262266
let lastMessage = self.messages[lastRowIndex]
263267

264268
self.editText(lastMessage.text)
265269

266-
self.tableView!.scrollToRowAtIndexPath(NSIndexPath(forRow: lastRowIndex, inSection: lastSectionIndex), atScrollPosition: .Bottom, animated: true)
270+
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: lastRowIndex, inSection: lastSectionIndex), atScrollPosition: .Bottom, animated: true)
267271
}
268272

269273
func togglePIPWindow(sender: AnyObject) {
@@ -382,16 +386,16 @@ extension MessageViewController {
382386
let rowAnimation: UITableViewRowAnimation = self.inverted ? .Bottom : .Top
383387
let scrollPosition: UITableViewScrollPosition = self.inverted ? .Bottom : .Top
384388

385-
self.tableView!.beginUpdates()
389+
self.tableView.beginUpdates()
386390
self.messages.insert(message, atIndex: 0)
387-
self.tableView!.insertRowsAtIndexPaths([indexPath], withRowAnimation: rowAnimation)
388-
self.tableView!.endUpdates()
391+
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: rowAnimation)
392+
self.tableView.endUpdates()
389393

390-
self.tableView!.scrollToRowAtIndexPath(indexPath, atScrollPosition: scrollPosition, animated: true)
394+
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: scrollPosition, animated: true)
391395

392396
// Fixes the cell from blinking (because of the transform, when using translucent cells)
393397
// See https://github.com/slackhq/SlackTextViewController/issues/94#issuecomment-69929927
394-
self.tableView!.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
398+
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
395399

396400
super.didPressRightButton(sender)
397401
}
@@ -434,7 +438,7 @@ extension MessageViewController {
434438
override func didCommitTextEditing(sender: AnyObject) {
435439

436440
self.editingMessage.text = self.textView.text
437-
self.tableView!.reloadData()
441+
self.tableView.reloadData()
438442

439443
super.didCommitTextEditing(sender)
440444
}
@@ -555,7 +559,7 @@ extension MessageViewController {
555559

556560
func messageCellForRowAtIndexPath(indexPath: NSIndexPath) -> MessageTableViewCell {
557561

558-
let cell = self.tableView!.dequeueReusableCellWithIdentifier(MessengerCellIdentifier) as! MessageTableViewCell
562+
let cell = self.tableView.dequeueReusableCellWithIdentifier(MessengerCellIdentifier) as! MessageTableViewCell
559563

560564
if cell.gestureRecognizers?.count == nil {
561565
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(MessageViewController.didLongPressCell(_:)))
@@ -572,7 +576,7 @@ extension MessageViewController {
572576

573577
// Cells must inherit the table view's transform
574578
// This is very important, since the main table view may be inverted
575-
cell.transform = self.tableView!.transform
579+
cell.transform = self.tableView.transform
576580

577581
return cell
578582
}

0 commit comments

Comments
 (0)