Skip to content

Instantly share code, notes, and snippets.

@adelevie
Created July 4, 2012 16:09
Show Gist options
  • Select an option

  • Save adelevie/3048083 to your computer and use it in GitHub Desktop.

Select an option

Save adelevie/3048083 to your computer and use it in GitHub Desktop.
class MyGroupedListController < UIViewController
attr_accessor :posts
attr_accessor :sections
class TableViewSection
attr_accessor :title
attr_accessor :items
def initialize(params={})
@title = params[:title]
@items = params[:items]
end
end
class TableViewSectionItem
attr_accessor :label
attr_accessor :details
attr_accessor :object
attr_accessor :selected
def initialize(params={})
@label = params[:label]
@details = params[:details]
@object = params[:object]
@selected = params[:selected]
end
end
def init
if super
self.tabBarItem = UITabBarItem.alloc.initWithTitle('Home', image:UIImage.imageNamed('list.png'), tag:1)
self.posts ||= User.current_user.posts
self.sections ||= []
posts_section = self.games.map do |game|
TableViewSectionItem.new(
:label => post.title,
:details => post.description,
:object => post,
:selected => Proc.new {
@post_view_controller ||= PostViewController.alloc.init
navigationController.pushViewController(@post_view_controller, animated:true)
@post_view_controller.show_post(post)
}
)
end
self.sections[0] = TableViewSection.new(
:title => "Posts",
:items => posts_section
)
from_draft_section_item = TableViewSectionItem.new(
:label => "From Draft",
:selected => Proc.new {
puts "TODO: implement new post from draft (from the proc!)"
}
)
from_scratch_section_item = TableViewSectionItem.new(
:label => "From Scratch",
:selected => Proc.new {
post = Post.new(:author => User.current_user)
@post_view_controller ||= PostViewController.alloc.init
@post_view_controller.show_post_editor(post)
}
)
self.sections[1] = TableViewSection.new(
:title => "New Post",
:items => [from_scratch_section_item, from_draft_section_item]
)
logout_section_item = TableViewSectionItem.new(
:label => "Logout",
:selected => Proc.new {
PFUser.logOut
loggedOutViewController = LoggedOutViewController.alloc.init
self.presentViewController(loggedOutViewController, animated:true, completion:nil)
}
)
self.sections[2] = TableViewSection.new(
:title => "Account",
:items => [logout_section_item]
)
end
self
end
def viewDidLoad
super
view.backgroundColor = UIColor.whiteColor
@myTableView = UITableView.alloc.initWithFrame(view.bounds, style:UITableViewStyleGrouped)
@myTableView.dataSource = self
@myTableView.delegate = self
@myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
view.addSubview(@myTableView)
end
def numberOfSectionsInTableView(tableView)
self.sections.length
end
def tableView(tableView, titleForHeaderInSection:section)
self.sections[section].title
end
def viewDidAppear(animated)
super
end
def viewWillAppear(animated)
navigationItem.title = "Home"
end
def viewDidUnload
@myTableView = nil
end
def tableView(tableView, didSelectRowAtIndexPath:indexPath)
section = self.sections[indexPath.section]
item = section.items[indexPath.row]
item.selected.call
end
# Sets the number of rows in each section
def tableView(tableView, numberOfRowsInSection:section)
section = self.sections[section]
section.items.length
end
# Modifies each cell
CellID = 'CellIdentifier'
def tableView(tableView, cellForRowAtIndexPath:indexPath)
cell = tableView.dequeueReusableCellWithIdentifier(CellID) || UITableViewCell.alloc.initWithStyle(UITableViewCellStyleSubtitle, reuseIdentifier:CellID)
section = self.sections[indexPath.section]
item = section.items[indexPath.row]
cell.textLabel.text = item.label
cell.detailTextLabel.text = item.details unless item.details.nil?
cell
end
end
@cmrichards
Copy link
Copy Markdown

What does this do?

@beathyate
Copy link
Copy Markdown

I'm guessing this line (#36):

posts_section = self.games.map do |game|

should be:

posts_section = self.posts.map do |post|

right?

@adelevie
Copy link
Copy Markdown
Author

adelevie commented Jul 16, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment