Skip to content

Instantly share code, notes, and snippets.

@noeticpenguin
Created October 10, 2013 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noeticpenguin/6925362 to your computer and use it in GitHub Desktop.
Save noeticpenguin/6925362 to your computer and use it in GitHub Desktop.
class RootViewController < UITableViewController
attr_accessor :dataRows
def didReceiveMemoryWarning
super
end
def dealloc
@dataRows = nil
end
def viewDidLoad
super
self.title = "Dreamforce Demo App"
request = SFRestAPI.sharedInstance.requestForQuery("SELECT Id, FirstName, LastName from Contact")
SFRestAPI.sharedInstance.send(request, delegate: self)
end
# SFRestAPI protocol methods
def request(request, didLoadResponse: jsonResponse)
records = jsonResponse.objectForKey("records")
@dataRows = reindex_table_data(records)
self.tableView.reloadData
end
def request(request, didFailLoadWithError: error)
puts "Request:DidFailLoadWithError: #{error}"
# @todo: better error handling here.
end
def requestDidCancelLoad(request)
puts "Request:requestDidCancelLoad: #{request}"
# @todo: better error handling here.
end
def requestDidTimeout(request)
puts "Request:requestDidTimeout: #{request}"
# @todo: better error handling here.
end
def reindex_table_data(incoming)
tmp = {}
("A".."Z").each do |l|
tmp[l] = incoming.select {|i| i["LastName"].starts_with? l}
end
tmp
end
# UITableView Protocol methods
def numberOfSectionsInTableView(tableView)
return 1
end
def dataRows
@dataRows = {} if @dataRows.nil?
@dataRows
end
def tableView(tableView, numberOfRowsInSection:section)
return @dataRows.count
end
def sections
dataRows.keys.sort
end
def row_for_section(section_index)
ap section_index
ap dataRows.keys.sort
ap dataRows[self.sections[section_index]]
dataRows[self.sections[section_index]]
end
def row_for_index_path(index_path)
ap index_path.section
row_for_section(index_path.section)[index_path.row]
end
def tableView(tableView, titleForHeaderInSection:section)
sections[section]
end
def sectionIndexTitlesForTableView(tableView)
sections
end
def tableView(tableView, sectionForSectionIndexTitle: title, atIndex: index)
sections.index title
end
def tableView(tableView, cellForRowAtIndexPath: indexPath)
@reuseIdentifier ||= "CELL_IDENTIFIER"
cell = tableView.dequeueReusableCellWithIdentifier(@reuseIdentifier)
cell = UITableViewCell.alloc.initWithStyle(UITableViewCellStyleValue1, reuseIdentifier:@reuseIdentifier) if cell.nil?
image = UIImage.imageNamed('glyphicons_003_user.png')
cell.imageView.image = image
# Have the cell show data.
rowObj = row_for_index_path(indexPath)
ap rowObj
cell.textLabel.text = "#{rowObj["FirstName"]} #{rowObj["LastName"]}"
#add the arrow to the right hand side.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator
cell
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment