jamonholmgren / ProMotion

ProMotion is a RubyMotion gem that makes iPhone development less like Objective-C and more like Ruby.
MIT License
1.26k stars 148 forks source link

delete_row fails when sugarcube's involved #233

Closed wejn closed 10 years ago

wejn commented 11 years ago

Hi,

I've found out that deleting rows fails when sugarcube gem is included.

The trace I'm getting is:

2013-07-17 10:03:21.127 Odorik[5765:c07] table_data.rb:25:in `cell:': undefined method `section' for 0:Fixnum (NoMethodError)
        from table.rb:114:in `block in delete_row:'
        from table.rb:111:in `delete_row:'
        from table.rb:227:in `tableView:commitEditingStyle:forRowAtIndexPath:'
2013-07-17 10:03:21.129 Odorik[5765:c07] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'table_data.rb:25:in `cell:': undefined method `section' for 0:Fixnum (NoMethodError)
        from table.rb:114:in `block in delete_row:'
        from table.rb:111:in `delete_row:'
        from table.rb:227:in `tableView:commitEditingStyle:forRowAtIndexPath:'

which doesn't make much sense, as there shouldn't be Fixnum passed as index_path to PM::TableData#cell(params={}).

Through experimenting I found out that the following line in delete_row is the offender:

index_paths = Array(index_paths)

Changing it to:

index_paths = [index_paths] if index_paths.kind_of?(NSIndexPath)

resolves the issue for me.

This auto-conversion of NSIndexPath to Array (through the fact sugarcube supplies nsindex.to_a) makes any Array(index_path) construct potentially unsafe.

Any take as to how to resolve this the proper way? (for now I've monkeypatched PM source for myself)

wejn commented 11 years ago

Just in case anyone runs into the same problem till there's official fix... this:

# remove following monkeypatch when ProMotion issue #233 is resolved
class ProMotion::Table
    alias_method :delete_row_buggy, :delete_row
    def delete_row(index_paths, animation = nil)
      index_paths = [index_paths] if index_paths.kind_of?(NSIndexPath)
      delete_row_buggy(index_paths, animation)
    end
end

will make the problem go away.

jamonholmgren commented 11 years ago

Looks good to me. Can you submit a PR to master?

wejn commented 11 years ago

Sent.

nicolasbrechet commented 10 years ago

Hello,

I have apparently the same issue.

When I add sugarcube in my gem list, deleting from a table ends up with "undefined method `section' for 0:Fixnum (NoMethodError)" Removing sugarcube, no error when deleting. With sugarcube and wejn's fix, no issue.

Is it something you're aware of ?

jamonholmgren commented 10 years ago

Looks like that fix disappeared in the PM 2.0 refactor. Added it back in (PR open).

nicolasbrechet commented 10 years ago

:thumbsup:

rokugou commented 8 years ago

I have same problem with ProMotion (2.5.0) sugarcube (3.3.7)

i added a monkey patch for temporary fix


The fix is work, but getting wrong index (0).

module ProMotion
  class TableData
    def cell(params={})
      if params.is_a?(Hash) && params[:index_path]
        if params[:index_path].is_a?(Fixnum)
          params[:index_path] = [0, params[:index_path]].nsindexpath
        end
      end
      params = index_path_to_section_index(params)
      table_section = params[:unfiltered] ? self.data[params[:section]] : self.section(params[:section])
      c = table_section[:cells].at(params[:index].to_i)
      set_data_cell_defaults(c)
    end
  end
end

I cannot found 'cell' called from where.

jamonholmgren commented 8 years ago

That's weird.