Skip to content

Instantly share code, notes, and snippets.

@Omnipresent
Created April 3, 2014 18:58
Show Gist options
  • Save Omnipresent/9960644 to your computer and use it in GitHub Desktop.
Save Omnipresent/9960644 to your computer and use it in GitHub Desktop.
class CategoryCell < UITableViewCell
def rmq_build
rmq(self.contentView).tap do |q|
# Add your subviews, init stuff here
# @foo = q.append(UILabel, :foo).get
#
# Or use the built-in table cell controls, if you don't use
# these, they won't exist at runtime
# q.build(self.imageView, :cell_image)
@name = q.build(self.textLabel, :cell_label).get
#q.append(self.detailTextLabel, "test")
end
end
def update(data)
# Update data here
@name.text = data[:name]
@name.detailTextLabel = "test"
# @name.detail_text = "what"
end
end
@twerth
Copy link

twerth commented Apr 3, 2014

Self in a block is risky, and unpredictable, and I think there are some bugs with it too.

What is the exception?

@Omnipresent
Copy link
Author

There is no exception. It just errors out with ((null))>. I've uploaded a sample project that was created with rmq and generated table view controller with rmq create table_view_controller item Its on github here: https://github.com/Omnipresent/rmqtest The item_cell.rb is the class that is having issues with detailTextLabel

My ultimate goal is to be able to customize my cell so that I can add buttons inside it (favorite button, delete button, share button, etc). But I figured I'll start with the built-in detailTextLabel first.

I'd appreciate if you can guide me in the right direction.

@twerth
Copy link

twerth commented Apr 4, 2014

If you need another style (UITableViewCellStyle ) other than UITableViewCellStyleDefault you'll have to create the cell in the table yourself (use build instead of create). Using initWithStyle when creating the cell.

If you can, paste your table code also.

@Omnipresent
Copy link
Author

Ok, cool. I'll take a look at implementing a UITableViewCellStyle. However, I still can't get detailTextLabel to work...:(

Here is the table code. It is basically everything that gets generated from rmq create table_view_controller item

class ItemController < UITableViewController

  ITEM_CELL_ID = "ItemCell"

  def viewDidLoad
    super

    load_data

    rmq.stylesheet = ItemControllerStylesheet

    view.tap do |table|
      table.delegate = self
      table.dataSource = self
      rmq(table).apply_style :table
    end
  end

  def load_data
    @data = 0.upto(rand(100)).map do |i| # Test data
      {
        name: %w(Lorem ipsum dolor sit amet consectetur adipisicing elit sed).sample,
        num: rand(100),
      }
    end
  end

  def tableView(table_view, numberOfRowsInSection: section)
    @data.length
  end

  def tableView(table_view, heightForRowAtIndexPath: index_path)
    rmq.stylesheet.item_cell_height
  end

  def tableView(table_view, cellForRowAtIndexPath: index_path)
    data_row = @data[index_path.row]

    cell = table_view.dequeueReusableCellWithIdentifier(ITEM_CELL_ID) || begin
      rmq.create(ItemCell, :item_cell, reuse_identifier: ITEM_CELL_ID).get
    end

    cell.update(data_row)
    cell
  end

  # Remove if you are only supporting portrait
  def supportedInterfaceOrientations
    UIInterfaceOrientationMaskAll
  end

  # Remove if you are only supporting portrait
  def willAnimateRotationToInterfaceOrientation(orientation, duration: duration)
    rmq.all.reapply_styles
  end
end

@twerth
Copy link

twerth commented Apr 6, 2014

When the table needs a cell, this line will be called:
rmq.create(ItemCell, :item_cell, reuse_identifier: ITEM_CELL_ID).get

rmq will create the view and .get passes back. However, RMQ does the most common kind of create. If you need to use some other init (initWithStyle) in this case, you can created it the old fashion way. Meaning just create the view normally using ItemCell.alloc.initWithStyle

If you want to initialize an existing view in rmq, use build, not create, some pseudo code:

view = ItemCell.alloc.initWithStyle(whatever params you want)
rmq.build(view, :item_cell)

This is true with any existing view, you can either append that view, prepend it, or if you just want to bring it into the rmq world, build it

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