infinitered / redpotion

We believe iPhone development should be clean, scalable, and fast with a language that developers not only enjoy, but actively choose. With the advent of Ruby for iPhone development the RubyMotion community has combined and tested the most active and powerful gems into a single package called RedPotion
MIT License
233 stars 40 forks source link

Cell height #103

Closed andersennl closed 9 years ago

andersennl commented 9 years ago

Hi, following example code:

module MyCellStylesheet
  def my_cell_height
    300
  end

  def my_cell(st)
    st.frame = { l: 5, t: 100, w: screen_width, h: my_cell_height }
  end
end

No matter what I enter as my_cell_height (e.g. 300 or 10), the actual height of the cell doesn't change. I've tried with different cells and different heights but nothing happens. As the redpotion generator generates the code regarding the height, I assume this is the correct (and only) place to change the height.

This is what the log tells me:

(#<UITableViewCellContentView:0...)> find(self).log

 object_id   | class                 | style_name              | frame                           |
 sv id       | superview             | subviews count          | tags                            |
 - - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
 4635218720  | UITableViewCellContent| content_view            | {l: 0, t: 0, w: 375, h: 43.5}   |
 4635216768  | MyCell            | 2                       |                                 |
 - - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
RMQ 4674630768. 1 selected. selectors: [#<UITableViewCellContentView:0x11447d320>]
=> nil

(#<UITableViewCellContentView:0...)> find(self).closest(MyCell).log

 object_id   | class                 | style_name              | frame                           |
 sv id       | superview             | subviews count          | tags                            |
 - - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |
 4635216768  | MyCell            | my_cell             | {l: 0, t: 155, w: 375, h: 44}   |
 4519189584  | UITableViewWrapperView| 3                       |                                 |
 - - - - - - | - - - - - - - - - - - | - - - - - - - - - - - - | - - - - - - - - - - - - - - - - |

What am I missing? Thanks!

squidpunch commented 9 years ago

I'll check into this a bit later today. I think that is a starting point for a cell height - but the table delegates are going to take over and resizing based on the content and/or defaults.

markrickert commented 9 years ago

UITableViewCell heights are not governed by stylesheets, they're governed by the UITableViewDelegate methods, which are handily abstracted for you in ProMotion, so if you're using a PM::TableScreen, just return a cell hash back with the height you want:

{
  title: "Whatever",
  action: :whatever,
  height: 70
}

Documentation here: https://github.com/clearsightstudio/ProMotion/blob/master/docs/Reference/API%20Reference%20-%20ProMotion%20Table%20-%20Cell%20Options.md

If You're using a standard old UITableView, you'll have to set the table's delegate to self and then implement this method: - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/

andersennl commented 9 years ago

Thanks a lot for your fast reply. I'm using a PM::TableScreen and I'm a little confused now. If the stylesheets dont have any influence on the cell, why does the redpotion generator generate the my_cell_height method? I thought it is supposed to regulate the cell's height?

markrickert commented 9 years ago

The generator is meant to give you a place to set the height in a stylesheet and then reference it in your cell hash in the controller, essentially keeping all your view logic all in the same place.

Check out the comments at the end of the generated cell view: potion create table_screen_cell bar_cell then look in /app/views/bar_cell.rb

You can use this like so in your table_screen:

  def table_data
    [
      {
        title: "Section",
        cells: [
          { cell_class: BarCell, height: stylesheet.bar_cell_height, title: "Foo"},
          { cell_class: BarCell, height: stylesheet.bar_cell_height, title: "Bar"}
        ]
      }
    ]
  end
andersennl commented 9 years ago

Sorry, I don't mean to annoy you but I stil find this confusing. I just created a test cell, this is the generated code:

module TestCellStylesheet
  def test_cell_height
    40
  end

  def test_cell(st)
    st.frame = {l: 5, t: 100, w: 80, h: test_cell_height}  # <--- this is unusable, right?
    st.background_color = color.light_gray

    # Style overall view here
  end

  def test_cell_title(st)
    st.frame = {l: 10, fr: 0, centered: :vertical, h: 20}
    st.font = font.medium
    st.color = color.black
  end
end
...

I use this code in my controller:

  def table_data
    [
      {
        title: "test",
        cells: [
          { cell_class: TestCell, height: stylesheet.test_cell_height, # line 20
          },
        ]
      },
    ]
  end

Then this is the error I get:

*** Terminating app due to uncaught exception 'NoMethodError', reason: 'settings_table_screen.rb:20:in `table_data': undefined method `test_cell_height' for nil:NilClass (NoMethodError)
    from table.rb:31:in `promotion_table_data'
    from table.rb:94:in `editable?'
    from table.rb:103:in `set_up_accessibility'
    from table.rb:23:in `screen_setup'
    from support.rb:17:in `try:'
    from screen_module.rb:20:in `screen_init:'
    from table_view_controller.rb:5:in `new:'
    from app_delegate.rb:9:in `on_load:'
    from delegate_module.rb:16:in `application:didFinishLaunchingWithOptions:'
markrickert commented 9 years ago

No problem at all... did you include that module in your stylesheet for the PM::TableScreen?

Should be something like:

class SettingsTableScreenStylesheet < ApplicationStylesheet
  include TestCellStylesheet # if this line isn't here, your tablescreen won't know about the new cell's style

  # other styles here
end

and in your tablescreen:

class SettingsTableScreen < PM::tableScreen
  stylesheet SettingsTableScreenStylesheet

  # other stuff here
end
squidpunch commented 9 years ago

hey @andersennl no annoyance at all!

*\ Terminating app due to uncaught exception 'NoMethodError', reason: 'settings_table_screen.rb:20:in table_data': undefined methodtest_cell_height' for nil:NilClass (NoMethodError)

So when this is fired the stylesheet is nil. do you have the stylesheet set in the screen like @markrickert listed above?

andersennl commented 9 years ago

Yes, everything is included like in the example above. I was about to write that about the NoMethodError from the nil class as well. I've double checked the names... this is so weird..

squidpunch commented 9 years ago

ok, let me see if I can reproduce this, unless you have a public repo with the problem I can pull down and help debug?

andersennl commented 9 years ago

That you both so much for your time. My project isn't online, it's mostly really basic, though. If you generate a table and a cell, you basically have my code ;)

squidpunch commented 9 years ago

@andersennl throwing together a quick project and see if I can help figure it out :+1:

squidpunch commented 9 years ago

im going to reopen this issue - 95% sure I can reproduce this bug!

I'll try and get a PR out to fix it this evening, sadly I cant work on it now as I have prior plans - but I have a local repo that I can reproduce the bug - thanks for reporting this @andersennl

andersennl commented 9 years ago

You are welcome, thanks again for your help!

squidpunch commented 9 years ago

@andersennl as a work around, you should be able to do this.

 def table_data
    [
      {
        title: "test",
        cells: [
          { cell_class: TestCell, height: 40
          },
        ]
      },
    ]
  end
andersennl commented 9 years ago

Yes that works, thanks! @squidpunch

squidpunch commented 9 years ago

@markrickert if you, or someone else wants to work on this before I get to it, feel free!

Essentially the problem is an order of operations. When we call table_data the stylesheet is not set, its being set later in the process - but we've got to figure out why it,s not set before table_data

I can check it out later tonight (or more likely tomorrow AM) if someone doesn't work on a PR before I get to it :smile: