3

I am using STCollapseTableView for expand/collapse tableView cells. I've integrated it through cocoapods and imported it in Bridging-Header. In my VC:

class MyVC: UITableViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView = STCollapseTableView()
  }

}

I have assigned the tableView to be STCollapsableTableView. I am getting two issues:

  1. self.tableView is of type UITableView and not of STCollapsableTableView.
  2. I am not able to call the functions defined in STCollapsableTableView like - (void)openSection:(NSUInteger)sectionIndex animated:(BOOL)animated; I think that I have to subclass my current VC to be able to use these, but I am really not sure on how to do it.

I read this question, but it doesn't help me. I want to override UITableView with STCollapseTableView in MyVC and use all the methods defined in it. Also, MyVC has to be a subclass of UITableViewController because of structural dependency. Please suggest a way to achieve it.

I am using Swift 2.3

Community
  • 1
  • 1
saurabh
  • 6,687
  • 7
  • 42
  • 63

3 Answers3

0

You can use a computed property in MyVC:

private var collapseTableView: STCollapseTableView {
    return tableView as! STCollapseTableView
}

You can also store it if you access it often, to avoid many casts. The rest is OK :)

aelveon
  • 132
  • 1
  • 1
  • 9
  • I am getting the following error: `Could not cast value of type 'UITableView' to 'STCollapseTableView'` – saurabh Apr 12 '16 at 08:17
  • Can you check if `STCollapseTableView` is a subclass of `UITableView`? It shouldn't give you this error, especially since you can do `self.tableView = STCollapseTableView()`. – aelveon Apr 12 '16 at 08:21
  • Yes it is a subclass of `UITableView` – saurabh Apr 12 '16 at 08:23
  • I just did the same thing and it worked, so there must be something different on your side. You're getting this error at compile time, right? On which line? Also, does `self.tableView = STCollapseTableView()` work properly? – aelveon Apr 12 '16 at 20:08
  • Its fine when I just declare it as above. But it crashes once I try to do this: `collapseTableView.openSection(sectionToExpand, animated: true)` with the error `Could not cast value of type 'UITableView' to 'STCollapseTableView'` – saurabh Apr 13 '16 at 07:32
  • It looks like you're trying to call `openSection` before setting `tableView` to an instance of `STCollapseTableView`. Place breakpoints on `self.tableView = STCollapseTableView()` and on `collapseTableView.openSection(sectionToExpand, animated: true)` to see where it stops first. – aelveon Apr 13 '16 at 07:51
0

In storyboard go to your tableViewController click on tableView of your tableViewController go to Identity Inspector and enter STCollapseTableView in the class field.

After this you should be good.

0

You'll want to override the loadView method. Check out the swift example given here https://stackoverflow.com/a/32107450/2741745

Community
  • 1
  • 1
Eric Alford
  • 926
  • 7
  • 11