Skip to content

Instantly share code, notes, and snippets.

@adelevie
Created November 17, 2012 18:26
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save adelevie/4098535 to your computer and use it in GitHub Desktop.
Save adelevie/4098535 to your computer and use it in GitHub Desktop.
Programmatically customize UITableViewCells in RubyMotion
# code inspired from http://jainmarket.blogspot.com/2009/05/creating-custom-table-view-cell.html
class CustomCell < UITableViewCell
attr_accessor :primaryLabel
attr_accessor :secondaryLabel
def createLabels
@primaryLabel = UILabel.alloc.init
@primaryLabel.textAlignment = UITextAlignmentLeft
@primaryLabel.font = UIFont.systemFontOfSize(14)
@secondaryLabel = UILabel.alloc.init
@secondaryLabel.textAlignment = UITextAlignmentLeft
@secondaryLabel.font = UIFont.systemFontOfSize(8)
self.contentView.addSubview(@primaryLabel)
self.contentView.addSubview(@secondaryLabel)
self
end
def layoutSubviews
super
contentRect = self.contentView.bounds
boundsX = contentRect.origin.x
@primaryLabel.frame = CGRectMake(boundsX+70, 5, 200, 25)
@secondaryLabel.frame = CGRectMake(boundsX+70, 30, 100, 15)
end
end
class TweetListController < UITableViewController
#...
CELLID = "CellIdentifier"
def tableView(tableView, cellForRowAtIndexPath:indexPath)
cell = tableView.dequeueReusableCellWithIdentifier(CELLID) || begin
cell = CustomCell.alloc.initWithStyle(UITableViewCellStyleDefault, reuseIdentifier:CELLID)
cell.createLabels
cell
end
tweet = @tweets[indexPath.row]
cell.primaryLabel.text = tweet.username
cell.secondaryLabel.text = tweet.text
cell
end
#...
end
@nco-webdev
Copy link

Good gist, thank you !

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