Welcome to my iphone development code blog

Welcome to my iphone development code blog
"SIMPLICITY IS BEST PROFESSION"

Thursday, July 15, 2010

customize cell

make a class

// CustomCell.h
// iwebservice
//

#import


@interface CustomCell : UITableViewCell {
UILabel *primaryLabel;

UILabel *secondaryLabel;

UIImageView *myImageView;
}
@property(nonatomic,retain)UILabel *primaryLabel;

@property(nonatomic,retain)UILabel *secondaryLabel;

@property(nonatomic,retain)UIImageView *myImageView;
@end

now put the code in its m file

// CustomCell.m
// iwebservice


#import "CustomCell.h"


@implementation CustomCell
@synthesize primaryLabel,secondaryLabel,myImageView;
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {

if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {

// Initialization code

primaryLabel = [[UILabel alloc]init];

primaryLabel.textAlignment = UITextAlignmentLeft;

primaryLabel.font = [UIFont systemFontOfSize:14];

secondaryLabel = [[UILabel alloc]init];

secondaryLabel.textAlignment = UITextAlignmentLeft;

secondaryLabel.font = [UIFont systemFontOfSize:8];

myImageView = [[UIImageView alloc]init];

[self.contentView addSubview:primaryLabel];

[self.contentView addSubview:secondaryLabel];

[self.contentView addSubview:myImageView];

}

return self;

}

- (void)layoutSubviews {

[super layoutSubviews];

CGRect contentRect = self.contentView.bounds;

CGFloat boundsX = contentRect.origin.x;

CGRect frame;

frame= CGRectMake(boundsX+10 ,0, 50, 50);

myImageView.frame = frame;

frame= CGRectMake(boundsX+70 ,5, 200, 25);

primaryLabel.frame = frame;

frame= CGRectMake(boundsX+70 ,30, 100, 15);

secondaryLabel.frame = frame;

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected animated:animated];

// Configure the view for the selected state
}


- (void)dealloc {
[super dealloc];
}


@end

now you can easily use it in your code
example,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

if(searching)
{
cell.primaryLabel.text = [copyListofItems objectAtIndex:indexPath.row];
}
else
{
if(indexPath.section ==0)
{
return nil;
}

NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section-1];
NSArray *array = [dictionary objectForKey:@"Country"];
NSString *cellValue = [array objectAtIndex:indexPath.row];

cell.primaryLabel.text = cellValue;

}

cell.primaryLabel.backgroundColor = [UIColor clearColor];
cell.primaryLabel.textColor = [UIColor colorWithRed:139.0/256 green:196.0/256 blue:40.0/256 alpha:100];
cell.primaryLabel.highlightedTextColor = [UIColor blackColor];

cell.secondaryLabel.text =[finalsgn objectForKey:[final objectForKey:cell.primaryLabel.text] ];
cell.secondaryLabel.backgroundColor = [UIColor clearColor];
cell.secondaryLabel.textColor = [UIColor colorWithRed:144.0/256 green:154.0/256 blue:47.0/256 alpha:100];
cell.secondaryLabel.highlightedTextColor = [UIColor blackColor];

UIImageView *cellBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"add-cell.jpg"]];
[cell setBackgroundView:cellBG];
[cellBG release];

UIImageView *cellbkBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"add-flate.jpg"]];
cell.selectedBackgroundView =cellbkBG;
cell.selectedBackgroundView.clearsContextBeforeDrawing = NO;
[cellbkBG release];

[cell.backgroundView setNeedsDisplay];

return cell;

}

enjoy !

No comments:

Post a Comment