Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
Overrides tableView in the Swift sample project, to avoiding unwrappi…
Browse files Browse the repository at this point in the history
…ng too many times.
  • Loading branch information
Ignacio Romero Zurbuchen committed Apr 27, 2016
1 parent e5be026 commit 35d7df9
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions Examples/Messenger-Swift/MessageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class MessageViewController: SLKTextViewController {

var editingMessage = Message()

override var tableView: UITableView {
get {
return super.tableView!
}
}


// MARK: - Initialisation

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

func commonInit() {

NSNotificationCenter.defaultCenter().addObserver(self.tableView!, selector: #selector(UITableView.reloadData), name: UIContentSizeCategoryDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self.tableView, selector: #selector(UITableView.reloadData), name: UIContentSizeCategoryDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MessageViewController.textInputbarDidMove(_:)), name: SLKTextInputbarDidMoveNotification, object: nil)

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

if let tableView = self.tableView {
tableView.separatorStyle = .None
tableView.registerClass(MessageTableViewCell.classForCoder(), forCellReuseIdentifier: MessengerCellIdentifier)
}
self.tableView.separatorStyle = .None
self.tableView.registerClass(MessageTableViewCell.classForCoder(), forCellReuseIdentifier: MessengerCellIdentifier)

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

self.tableView!.scrollToRowAtIndexPath(cell.indexPath, atScrollPosition: .Bottom, animated: true)
self.tableView.scrollToRowAtIndexPath(cell.indexPath, atScrollPosition: .Bottom, animated: true)
}

func editRandomMessage(sender: AnyObject) {
Expand All @@ -256,14 +260,14 @@ extension MessageViewController {
return
}

let lastSectionIndex = self.tableView!.numberOfSections-1
let lastRowIndex = self.tableView!.numberOfRowsInSection(lastSectionIndex)-1
let lastSectionIndex = self.tableView.numberOfSections-1
let lastRowIndex = self.tableView.numberOfRowsInSection(lastSectionIndex)-1

let lastMessage = self.messages[lastRowIndex]

self.editText(lastMessage.text)

self.tableView!.scrollToRowAtIndexPath(NSIndexPath(forRow: lastRowIndex, inSection: lastSectionIndex), atScrollPosition: .Bottom, animated: true)
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: lastRowIndex, inSection: lastSectionIndex), atScrollPosition: .Bottom, animated: true)
}

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

self.tableView!.beginUpdates()
self.tableView.beginUpdates()
self.messages.insert(message, atIndex: 0)
self.tableView!.insertRowsAtIndexPaths([indexPath], withRowAnimation: rowAnimation)
self.tableView!.endUpdates()
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: rowAnimation)
self.tableView.endUpdates()

self.tableView!.scrollToRowAtIndexPath(indexPath, atScrollPosition: scrollPosition, animated: true)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: scrollPosition, animated: true)

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

super.didPressRightButton(sender)
}
Expand Down Expand Up @@ -434,7 +438,7 @@ extension MessageViewController {
override func didCommitTextEditing(sender: AnyObject) {

self.editingMessage.text = self.textView.text
self.tableView!.reloadData()
self.tableView.reloadData()

super.didCommitTextEditing(sender)
}
Expand Down Expand Up @@ -555,7 +559,7 @@ extension MessageViewController {

func messageCellForRowAtIndexPath(indexPath: NSIndexPath) -> MessageTableViewCell {

let cell = self.tableView!.dequeueReusableCellWithIdentifier(MessengerCellIdentifier) as! MessageTableViewCell
let cell = self.tableView.dequeueReusableCellWithIdentifier(MessengerCellIdentifier) as! MessageTableViewCell

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

// Cells must inherit the table view's transform
// This is very important, since the main table view may be inverted
cell.transform = self.tableView!.transform
cell.transform = self.tableView.transform

return cell
}
Expand Down

3 comments on commit 35d7df9

@yanjinww
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dzenbot Hi I'm using slack view controller in my app. I have a question about the updates to the tableView becoming an optional. In your example, you're force-unwrapping it, is it a recommended way of using it? isn't it crashing the app if it's nil?

@dzenbot
Copy link

@dzenbot dzenbot commented on 35d7df9 Jun 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @yanjinww: because tableView is an optional (since you can either initialise the view controller to be using a collectionView or scrollView) it is necessary to unwrap.
But you might find this useful as it will allow you to no unwrap everywhere, just once:
#450 (comment)

I should update the sample project with this solution instead.

@yanjinww
Copy link

@yanjinww yanjinww commented on 35d7df9 Jun 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @dzenbot your answer is very helpful. follow-up question:

override var tableView: UITableView {
        get {
            return super.tableView!
        }
    }

does the "return super.tableView!" guarantee to return non-nil value or still can be nil in the case of the code above?

Please sign in to comment.