You can now specify title_view for custom views for your section titles

def table_data
  [{
    title: "My section title",
    title_view: MyCustomSectionView,
    title_height: 50,
    cells: []
  }]
end

If you specify a title accessor on the custom view, this will be set to the
title

class MyCustomSectionView < UITableViewCell
  attr_accessor :title
end
This commit is contained in:
Jesper Christiansen
2013-07-13 18:22:37 -07:00
committed by Jamon Holmgren
parent 7232d350f6
commit 37dcc2c161
3 changed files with 58 additions and 5 deletions

View File

@@ -242,9 +242,33 @@ module ProMotion
PM.logger.warn "ProMotion expects you to use 'delete_cell(index_paths, animation)'' instead of 'deleteRowsAtIndexPaths(index_paths, withRowAnimation:animation)'."
delete_row(index_paths, animation)
end
# Section view methods
def tableView(table_view, viewForHeaderInSection: index)
section = section_at_index(index)
if section[:title_view]
klass = section[:title_view]
view = klass.new if klass.respond_to?(:new)
view.title = section[:title] if view.respond_to?(:title=)
view
else
nil
end
end
def tableView(table_view, heightForHeaderInSection: index)
section = section_at_index(index)
if section[:title_view] || (section[:title] && !section[:title].empty?)
section[:title_view_height] || tableView.sectionHeaderHeight
else
0.0
end
end
protected
def map_row_animation_symbol(symbol)
symbol ||= UITableViewRowAnimationAutomatic
{
@@ -288,7 +312,7 @@ module ProMotion
def get_refreshable_params
@refreshable_params ||= nil
end
# Indexable
def indexable(params = {})
@indexable_params = params

View File

@@ -0,0 +1,4 @@
class CustomTitleView < UITableViewCell
attr_accessor :title
end

View File

@@ -43,6 +43,10 @@ describe "PM::Table module" do
title: "Table cell group 3", cells: [ cell_factory(title: "3-1"), cell_factory(title: "3-2", background_color: UIColor.blueColor) ]
},{
title: "Table cell group 4", cells: [ custom_cell, cell_factory(title: "4-2"), cell_factory(title: "4-3"), cell_factory(title: "4-4") ]
},{
title: "Custom section title 1", title_view: CustomTitleView, cells: [ ]
},{
title: "Custom section title 2", title_view: CustomTitleView.new, title_view_height: 50, cells: [ ]
}]
end
@@ -55,7 +59,7 @@ describe "PM::Table module" do
end
it "should have the right number of sections" do
@subject.numberOfSectionsInTableView(@subject.table_view).should == 4
@subject.numberOfSectionsInTableView(@subject.table_view).should == 6
end
it "should set the section titles" do
@@ -63,6 +67,8 @@ describe "PM::Table module" do
@subject.tableView(@subject.table_view, titleForHeaderInSection:1).should == "Table cell group 2"
@subject.tableView(@subject.table_view, titleForHeaderInSection:2).should == "Table cell group 3"
@subject.tableView(@subject.table_view, titleForHeaderInSection:3).should == "Table cell group 4"
@subject.tableView(@subject.table_view, titleForHeaderInSection:4).should == "Custom section title 1"
@subject.tableView(@subject.table_view, titleForHeaderInSection:5).should == "Custom section title 2"
end
it "should create the right number of cells" do
@@ -70,6 +76,8 @@ describe "PM::Table module" do
@subject.tableView(@subject.table_view, numberOfRowsInSection:1).should == 1
@subject.tableView(@subject.table_view, numberOfRowsInSection:2).should == 2
@subject.tableView(@subject.table_view, numberOfRowsInSection:3).should == 4
@subject.tableView(@subject.table_view, numberOfRowsInSection:4).should == 0
@subject.tableView(@subject.table_view, numberOfRowsInSection:5).should == 0
end
it "should create the jumplist" do
@@ -102,7 +110,7 @@ describe "PM::Table module" do
@subject.tableView(@subject.table_view, didSelectRowAtIndexPath:@custom_ip)
end
it "should set a custom cell background image" do
@image.should.not.be.nil
ip = NSIndexPath.indexPathForRow(0, inSection: 3) # Cell 2-1
@@ -119,4 +127,21 @@ describe "PM::Table module" do
cell.backgroundColor.should == UIColor.blueColor
end
describe("section with custom title_view") do
it "should use the correct class for section view" do
cell = @subject.tableView(@subject.table_view, viewForHeaderInSection: 4)
cell.should.be.kind_of(CustomTitleView)
end
it "should use the default section height if none is specified" do
@subject.tableView(@subject.table_view, heightForHeaderInSection:4).should == 22.0 # Built-in default
end
it "should use the set title_view_height if one is specified" do
@subject.tableView(@subject.table_view, heightForHeaderInSection:5).should == 50.0
end
end
end