Skip to content

Instantly share code, notes, and snippets.

@gertig
Forked from arkan/gist:5662905
Last active December 19, 2015 23:59
Show Gist options
  • Save gertig/6038283 to your computer and use it in GitHub Desktop.
Save gertig/6038283 to your computer and use it in GitHub Desktop.
class MyCustomCell < UITableViewCell
# This method is used by ProMotion to instantiate cells.
def initWithStyle(style_name, reuseIdentifier: reuseIdentifier)
super
stylish
self
end
# A delegate method when the user clicks the Row(it's blue by default)
def setHighlighted(highlighted, animated: animated)
if highlighted
self.backgroundColor = UIColor.redColor
else
self.backgroundColor = UIColor.whiteColor
end
end
def stylish
self.backgroundColor = UIColor.whiteColor
self.selectionStyle = UITableViewCellSelectionStyleNone
end
def user=(user)
@user = user
self.textLabel.text = self.user[:screen_name]
self.detailTextLabel.text = self.user[:bio]
self.imageView.url = {url: self.user[:profile_image_url].to_url, placeholder: UIImage.imageNamed("placeholder-image")}
self.setNeedsLayout
@user
end
# This is useful if you have cells of varying heights depending on the content
def self.heightForCellWithUser(user)
if user
sizeToFit = user[:bio].sizeWithFont(UIFont.systemFontOfSize(12), constrainedToSize: CGSizeMake(220, Float::MAX), lineBreakMode:UILineBreakModeWordWrap)
return [70, sizeToFit.height + 45].max
else
return 10 # arbitrary height
end
end
def layoutSubviews
super
self.imageView.frame = CGRectMake(10, 10, 50, 50)
self.textLabel.frame = CGRectMake(70, 10, 240, 20)
detailTextLabelFrame = CGRectOffset(self.textLabel.frame, 0, 25);
detailTextLabelFrame.size.height = self.class.heightForCellWithUser(self.user) - 45
self.detailTextLabel.frame = detailTextLabelFrame
end
end
class MyAwesomeScreen < ProMotion::TableScreen
def on_init
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone #To remove the cell's separator
self.tableView.backgroundColor = UIColor.yellowColor
end
def table_data
[{
cells: @users.map {|user|
{
cell_identifier: "MyCustomCellIdentifier",
cell_class: MyCustomCell,
height: MyCustomCell.heightForCellWithUser(user) # maybe some users need more room than others
}
}
}]
end
# Override this method but call ProMotion's "table_view_cell" method so you can still use PM to set the table_data
# Then you can customize the cell before returning it to the tableView
# An example is passing in a user object so you can build a sweet custom cell for that User using a subclass of TableViewCell
def tableView(tableView, cellForRowAtIndexPath:index_path)
cell = table_view_cell(index_path: index_path)
cell.user = @people[index_path.row] if @people
cell
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment