Search code examples
iosuitableviewrubymotion

How to create facebook feed style cards in UITableView with Rubymotion?


I've been trying to recreate the facebook style feed using a UITableView without much success. My first idea was to add a new view to the cell that contained the 'card' but I have not been able to make this work. Does anyone have any suggestions on how I go about doing this?

My other concern is how do I make each cell vary in height dependant on content?

Attached is an image of the facebook UI to hopefully explain what I'm trying to achieve.

Facebook Feed UI


Solution

  • I would subclass UITableViewCell, extend initWithStyle:reuseIdentifier: with custom initialization code, and override layoutSubviews to position everything based on your variable content.

    Based on a label's content and its UIFont, you can calculate its width and height.

    'Wow! Nice Photo!'.sizeWithFont(UIFont.fontWithName('Helvetica', size: 18.0))
     # CGSize width=144.0 height=22.0>
    

    Here's a template for how I would setup a variable UITableViewCell subclass:

    class AwesomeCell < UITableViewCell
      def initWithStyle(style, reuseIdentifier: reuse_identifier)
        super.tap do |cell|
          # initialize and add your subviews to contentView
        end
      end
    
      def layoutSubview
        # layout your subviews
      end
    
      def calculated_height
        # calculate your height
      end      
    end
    

    But as aceofspades pointed out, you should be aware of the performance impact of dynamically determining the height of UITableViewCells.

    If you can get away with showing a fixed number of lines within the label to keep the cell's overall height fixed, you'd get much better performance.

    Even creating a few different fixed height UITableViewCell subclasses and using the appropriate one based on the incoming content would probably yield a more performant UITableView.