         }
     }
     
-    func apiURL(numHits: Int, page: Int) -> NSURL {
+    // MARK: - UITableViewDataSource
+    
+    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+        return stories.count
+    }
+    
+    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
+        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell
+        let story = stories[indexPath.row]
+        
+        cell.textLabel?.text = story.title
+        cell.detailTextLabel?.text = story.author
+        
+        if useAutosizingCells && tableView.respondsToSelector("layoutMargins") {
+            cell.textLabel?.numberOfLines = 0
+            cell.detailTextLabel?.numberOfLines = 0
+        }
+        
+        return cell
+    }
+    
+    // MARK: - UIAlertViewDelegate
+    
+    func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
+        if buttonIndex != alertView.cancelButtonIndex {
+            fetchData(nil)
+        }
+    }
+    
+    // MARK: - Private
+    
+    private func apiURL(numHits: Int, page: Int) -> NSURL {
         let string = "https://hn.algolia.com/api/v1/search_by_date?tags=story&hitsPerPage=\(numHits)&page=\(page)"
         let url = NSURL(string: string)
         
         return url!
     }
     
-    func fetchData(handler: ((Void) -> Void)?) {
+    private func fetchData(handler: ((Void) -> Void)?) {
         let hits: Int = Int(CGRectGetHeight(tableView.bounds)) / 44
         let requestURL = apiURL(hits, page: currentPage)
         

        tableView.infiniteScrollIndicatorMargin = 40
        
        // Add infinite scroll handler
        tableView.addInfiniteScrollWithHandler { [weak self] (scrollView: AnyObject!) -> Void in
            let scrollView = scrollView as! UIScrollView
            
            self?.fetchData() {
                scrollView.finishInfiniteScroll()

        })
    }
    
    private func handleResponse(data: NSData!, response: NSURLResponse!, error: NSError!) {
        if error != nil {
            showAlertWithError(error)
            return;
        }
        
        var jsonError: NSError?
        let responseDict = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &jsonError) as? Dictionary<String, AnyObject>
        
        if jsonError != nil {
            showAlertWithError(jsonError)
            return
        }
        
        if let pages = responseDict?[JSONNumPagesKey] as? NSNumber {
            numPages = pages as! Int
        }
        
        if let results = responseDict?[JSONResultsKey] as? Array<Dictionary<String, AnyObject>> {
            currentPage++

            for i in results {
                stories.append(StoryModel(i))
            }
            
            tableView.reloadData()
        }
    }
    
    private func showAlertWithError(error: NSError!) {
        let alert = UIAlertView(
            title: NSLocalizedString("Error fetching data", comment: ""),
            message: error.localizedDescription,
            delegate: self,
            cancelButtonTitle: NSLocalizedString("Dismiss", comment: ""),
            otherButtonTitles: NSLocalizedString("Retry", comment: "")
        )
        alert.show()
    }

}
