Skip to content

Instantly share code, notes, and snippets.

@hackedunit
Created December 17, 2014 19:22
Show Gist options
  • Save hackedunit/416543e8d0bf5cd368be to your computer and use it in GitHub Desktop.
Save hackedunit/416543e8d0bf5cd368be to your computer and use it in GitHub Desktop.
class BooksController < UITableViewController
def viewDidLoad
super
loadBooks
end
def numberOfSectionsInTableView(tableView)
return 1
end
def tableView(tableView, numberOfRowsInSection: section)
@books.count
end
def tableView(tableView, cellForRowAtIndexPath: indexPath)
book = @books[indexPath.row]
@reuseIdentifier ||= "BookCell"
cell = tableView.dequeueReusableCellWithIdentifier(@reuseIdentifier)
cell ||= CustomBookCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier: @reuseIdentifier)
cell.bookName.text = book[0].to_s
cell.authorName.text = book[1].to_s
cell.price.text = book[2].to_s
cell
end
def tableView(tableView, didSelectRowAtIndexPath: indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
end
private
def loadBooks
@books = [["Twelfth Night", "William Shakespeare", "$10"], ["Life on Earth", "E.O. Wilson", "$30"], ["Oliver Twist", "Charles Dickens", "$20"]]
end
end
class CustomBookCell < UITableViewCell
extend IB
outlet :bookName, UILabel
outlet :authorName, UILabel
outlet :price, UILabel
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment