Skip to content

Instantly share code, notes, and snippets.

@kishikawakatsumi
Created June 1, 2012 17:21
Show Gist options
  • Save kishikawakatsumi/2853777 to your computer and use it in GitHub Desktop.
Save kishikawakatsumi/2853777 to your computer and use it in GitHub Desktop.
#import "SDTableViewCell.h"
@implementation SDTableViewCell
@synthesize iconView;
@synthesize indicatorView;
@synthesize titleLabel;
- (void)dealloc {
[iconView removeObserver:self forKeyPath:@"image"];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.iconView = [[UIImageView alloc] initWithFrame:CGRectMake(2.0f, 2.0f, 40.0f, 40.0f)];
[self.contentView addSubview:iconView];
self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.indicatorView.hidesWhenStopped = YES;
self.indicatorView.frame = CGRectMake(12.0f, 12.0f, 20.0f, 20.0f);
[self.contentView addSubview:indicatorView];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(46.0f, 12.0f, 262.0f, 20.0f)];
[self.contentView addSubview:self.titleLabel];
[self.iconView addObserver:self forKeyPath:@"image" options:0 context:NULL];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (self.iconView.image) {
[self.indicatorView stopAnimating];
} else {
[self.indicatorView startAnimating];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (self.iconView.image) {
[self.indicatorView stopAnimating];
} else {
[self.indicatorView startAnimating];
}
}
@end
==
// Usage
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
SDTableViewCell *cell = (SDTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[SDTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.iconView setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]]];
return cell;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment