Mabedan
Mabedan

Reputation: 907

self.tableView not recognized as a UITableView, but rather a random method

I'm struggling with xCode for hours, over a very simple issue. I'm subclassing UITableViewController, and in the scrollViewDidScroll method, I try to access my visible tableview rows, as such:

class ShinySubclass: UITableViewController {
    override func scrollViewDidScroll(scrollView: UIScrollView) {
        let visibleIndexPaths : [NSIndexPath] = self.tableView.indexPathsForVisibleRows()
        ...
    }
}

although I get this lovely message, pointing to self.tableView:

ShinySubclass.swift:58:48: '(UITableView, numberOfRowsInSection: Int) -> Int' does not have a member named 'indexPathsForVisibleRows'

This seems like an xCode bug to me. I've got the latest version (Version 6.1 (6A1052d)), and I've tried anything from clean, rebuild, turn room lights on/off etc...

Any ideas how to get around it?

Upvotes: 0

Views: 1098

Answers (1)

Antonio
Antonio

Reputation: 72770

indexPathsForVisibleRows returns [AnyObject]?, but you are trying to assign to a non optional [NSIndexPath] variable. If you just let type inference do the job the code compiles:

let visibleIndexPaths  = self.tableView.indexPathsForVisibleRows()

That returns an array of AnyObject, so probably you want to downcast to the proper type, but in this case a downcast is needed:

let visibleIndexPaths = self.tableView.indexPathsForVisibleRows() as? [NSIndexPath]

However, since I presume you want to do some processing if that array is not nil, and taking into account that indexPathsForVisibleRows can return nil if no row is visible, you can use optional binding:

if let visibleIndexPaths = self.tableView.indexPathsForVisibleRows() as? [NSIndexPath] {
    ...   
}

In this case visibleIndexPaths is a non optional array [NSIndexPath]

Upvotes: 1

Related Questions