Gavin Morrice

is a web and iOS developer from Edinburgh, Scotland.

more about me »

Blog Archive

A helper for showing loading cells in iOS Apps with Rubymotion

I'm in the process of transposing an existing application from Objective-C to Rubymotion. In doing so, I've come up with a few techniques that make life a lot easier.

Here's a simple class for adding a loading cell to a UITableView

class LoadingCell < UITableViewCell

  CELL_FRAME = [[0,0], [320, 44]]
  LABEL_FRAME = [[120, 11], [79, 21]]
  ACTIVITY_INDICATOR_FRAME = [[92,11],[20,20]]

  LABEL_TEXT = 'Loading...'

  def self.loadingCellWithTableView(tableView)
    tableView.dequeueReusableCellWithIdentifier(self.class.to_s) || begin
      alloc.initWithFrame(CELL_FRAME)
    end
  end

  def reuseIdentifier
    self.class.to_s
  end

  def initWithFrame(frame)
    super(frame)
    addSubview(activityIndicator)
    addSubview(label)    
  end

  def label
    @label ||= begin
      _label           = UILabel.alloc.initWithFrame(LABEL_FRAME)
      _label.text      = LABEL_TEXT
      _label.textColor = UIColor.darkGrayColor
      _label.backgroundColor = UIColor.clearColor
      _label
    end
  end

  def activityIndicator
    @activityIndicator ||= begin
      _activityIndicator = UIActivityIndicatorView.alloc.initWithActivityIndicatorStyle(UIActivityIndicatorViewStyleGray)
      _activityIndicator.frame = ACTIVITY_INDICATOR_FRAME
      _activityIndicator.startAnimating
      _activityIndicator
    end
  end

end

And to use it:

# in my controller

def tableView(tableView, cellForRowAtIndexPath: indexPath)
  # return a loading cell
  LoadingCell.loadingCellWithTableView(self.tableView)
end

Here's how it looks:

Loading Cell

Feel free to modify to suit your needs :-)

Filed under: ruby, iOS, Apps, Rubymotion, Snippets


How would you stop the loading dialog and load the actual cell when it's ready?

Posted on by Alan