Skip to content

Instantly share code, notes, and snippets.

@arkan
Forked from adelevie/custom_cell.rb
Created May 23, 2013 09:54
Show Gist options
  • Save arkan/5635018 to your computer and use it in GitHub Desktop.
Save arkan/5635018 to your computer and use it in GitHub Desktop.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment