mirror of
https://github.com/zhigang1992/ProMotion.git
synced 2026-05-15 07:37:21 +08:00
Merge pull request #498 from clearsightstudio/update_table_data_index_paths
Allow users to pass an index_path or array of index_paths to update_table_data.
This commit is contained in:
28
app/test_screens/update_test_table_screen.rb
Normal file
28
app/test_screens/update_test_table_screen.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
class UpdateTestTableScreen < PM::TableScreen
|
||||
def table_data; @table_data ||= []; end
|
||||
def on_load
|
||||
@table_data = [{cells: []}]
|
||||
update_table_data
|
||||
end
|
||||
|
||||
def make_more_cells
|
||||
@table_data = [{cells: [{title: "Cell 1"},{title: "Cell 2"}]}]
|
||||
end
|
||||
|
||||
def change_cells
|
||||
@table_data = [{cells: [{title: "Cell A"},{title: "Cell B"}]}]
|
||||
end
|
||||
|
||||
def first_cell_title
|
||||
cell_title(0)
|
||||
end
|
||||
|
||||
def second_cell_title
|
||||
cell_title(1)
|
||||
end
|
||||
|
||||
def cell_title(index)
|
||||
ip = NSIndexPath.indexPathForRow(index, inSection:0)
|
||||
table_view.cellForRowAtIndexPath(ip).textLabel.text
|
||||
end
|
||||
end
|
||||
@@ -62,9 +62,17 @@ module ProMotion
|
||||
self.promotion_table_data.search_string
|
||||
end
|
||||
|
||||
def update_table_view_data(data)
|
||||
def update_table_view_data(data, args = {})
|
||||
self.promotion_table_data.data = data
|
||||
table_view.reloadData
|
||||
if args[:index_paths]
|
||||
args[:animation] ||= UITableViewRowAnimationNone
|
||||
|
||||
table_view.beginUpdates
|
||||
table_view.reloadRowsAtIndexPaths(Array(args[:index_paths]), withRowAnimation:args[:animation])
|
||||
table_view.endUpdates
|
||||
else
|
||||
table_view.reloadData
|
||||
end
|
||||
@table_search_display_controller.searchResultsTableView.reloadData if searching?
|
||||
end
|
||||
|
||||
@@ -122,8 +130,11 @@ module ProMotion
|
||||
table_cell
|
||||
end
|
||||
|
||||
def update_table_data
|
||||
self.update_table_view_data(self.table_data)
|
||||
def update_table_data(args = {})
|
||||
# Try and detect if the args param is a NSIndexPath or an array of them
|
||||
args = { index_paths: args } if args.is_a?(NSIndexPath) || (args.is_a?(Array) && array_all_members_of?(args, NSIndexPath))
|
||||
|
||||
self.update_table_view_data(self.table_data, args)
|
||||
self.promotion_table_data.search(search_string) if searching?
|
||||
end
|
||||
|
||||
|
||||
@@ -2,12 +2,17 @@ module ProMotion
|
||||
module Table
|
||||
module Utils
|
||||
def index_path_to_section_index(params)
|
||||
if params[:index_path]
|
||||
if params.is_a?(Hash) && params[:index_path]
|
||||
params[:section] = params[:index_path].section
|
||||
params[:index] = params[:index_path].row
|
||||
end
|
||||
params
|
||||
end
|
||||
|
||||
# Determines if all members of an array are a certain class
|
||||
def array_all_members_of?(arr, kalss)
|
||||
arr.select{ |e| e.is_a?(klass) }.length == arr.length
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
50
spec/functional/func_table_screen_updating_spec.rb
Normal file
50
spec/functional/func_table_screen_updating_spec.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
describe "ProMotion::TableScreen updating functionality" do
|
||||
tests PM::UpdateTestTableScreen
|
||||
|
||||
it 'should update a single row in the table view' do
|
||||
table_screen = UpdateTestTableScreen.new
|
||||
table_screen.make_more_cells
|
||||
table_screen.update_table_data
|
||||
table_screen.change_cells
|
||||
|
||||
table_screen.first_cell_title.should == "Cell 1"
|
||||
table_screen.second_cell_title.should == "Cell 2"
|
||||
|
||||
table_screen.update_table_data(NSIndexPath.indexPathForRow(0, inSection:0))
|
||||
|
||||
table_screen.first_cell_title.should == "Cell A"
|
||||
table_screen.second_cell_title.should == "Cell 2"
|
||||
end
|
||||
|
||||
it 'should allow multiple formats of index paths to be passed' do
|
||||
table_screen = UpdateTestTableScreen.new
|
||||
table_screen.make_more_cells
|
||||
table_screen.update_table_data
|
||||
table_screen.change_cells
|
||||
|
||||
# Single NSIndexPath
|
||||
Proc.new {
|
||||
table_screen.update_table_data(NSIndexPath.indexPathForRow(0, inSection:0))
|
||||
}.should.not.raise(StandardError)
|
||||
|
||||
# Array of NSIndexPaths
|
||||
Proc.new {
|
||||
table_screen.update_table_data([NSIndexPath.indexPathForRow(0, inSection:0), NSIndexPath.indexPathForRow(1, inSection:0)])
|
||||
}.should.not.raise(StandardError)
|
||||
|
||||
# # Hash with single NSIndexPath
|
||||
Proc.new {
|
||||
table_screen.update_table_data({index_paths: NSIndexPath.indexPathForRow(0, inSection:0)})
|
||||
}.should.not.raise(StandardError)
|
||||
|
||||
# Hash with array of NSIndexPaths
|
||||
Proc.new {
|
||||
table_screen.update_table_data({index_paths: [NSIndexPath.indexPathForRow(0, inSection:0), NSIndexPath.indexPathForRow(1, inSection:0)]})
|
||||
}.should.not.raise(StandardError)
|
||||
|
||||
# Hash with NSIndexPath and row animation
|
||||
Proc.new {
|
||||
table_screen.update_table_data({index_paths: NSIndexPath.indexPathForRow(0, inSection:0), animation: UITableViewRowAnimationFade})
|
||||
}.should.not.raise(StandardError)
|
||||
end
|
||||
end
|
||||
@@ -112,20 +112,6 @@ describe "table screens" do
|
||||
end
|
||||
|
||||
describe "test PM::TableScreen's updating functionality" do
|
||||
before do
|
||||
class UpdateTestTableScreen < PM::TableScreen
|
||||
def table_data; @table_data ||= []; end
|
||||
def on_load
|
||||
@table_data = [{cells: []}]
|
||||
update_table_data
|
||||
end
|
||||
|
||||
def make_more_cells
|
||||
@table_data = [{cells: [{title: "Cell 1"},{title: "Cell 2"}]}]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'should update the table data when update_table_data is called' do
|
||||
@screen = UpdateTestTableScreen.new
|
||||
@screen.tableView(@screen.tableView, numberOfRowsInSection:0).should == 0
|
||||
|
||||
34
spec/unit/tables/table_utils_spec.rb
Normal file
34
spec/unit/tables/table_utils_spec.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
describe "PM::Table utils" do
|
||||
|
||||
before do
|
||||
@subject = TestTableScreen.new
|
||||
@subject.on_load
|
||||
@subject.update_table_data
|
||||
end
|
||||
|
||||
it "should convert an index path to a section index" do
|
||||
index_path = NSIndexPath.indexPathForRow(12, inSection:2)
|
||||
given = {index_path: index_path}
|
||||
expected = {
|
||||
index_path: index_path,
|
||||
section: 2,
|
||||
index: 12
|
||||
}
|
||||
|
||||
@subject.index_path_to_section_index(given).should == expected
|
||||
end
|
||||
|
||||
it "return the original param when converting an index path with incorrect values" do
|
||||
@subject.index_path_to_section_index(17).should == 17
|
||||
end
|
||||
|
||||
it "should properly determine if all members of an array are the same class" do
|
||||
@subject.array_all_members_of?([1, 2, 3, 4], Fixnum).should == true
|
||||
@subject.array_all_members_of?(["string", 'string2'], String).should == true
|
||||
@subject.array_all_members_of?([:sym1, :sym2, :sym3], Symbol).should == true
|
||||
|
||||
@subject.array_all_members_of?([1, 2, 3, 4, 'String'], Fixnum).should == false
|
||||
@subject.array_all_members_of?([4.4, 2], Fixnum).should == false
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user