Displaying a custom cell in the UITable - iphone

I'm attempting to display my data, which is in a UITableView, in custom cells that I created. This is my custom cell class (CustomCell.m):
#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];
}
Then, I created the rootViewController that has the tableView in which the custom cells should be displayed (TeamsViewController.m):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 5;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Custom cell here
switch (indexPath.row) {
case 0:
cell.primaryLabel.text =#"Meeting on iPhone Development";
cell.secondaryLabel.text = #"Sat 10:30";
cell.myImageView.image = [UIImage imageNamed:#"Icon4.png"];
break;
case 1:
cell.primaryLabel.text = #"Call With Client";
cell.secondaryLabel.text = #"Planned";
cell.myImageView.image = [UIImage imageNamed:#"Icon4.png.png"];
break;
case 2:
cell.primaryLabel.text = #"Appointment with Joey";
cell.secondaryLabel.text = #"2 Hours";
cell.myImageView.image = [UIImage imageNamed:#"Icon4.png.png"];
break;
case 3:
cell.primaryLabel.text = #"Call With Client";
cell.secondaryLabel.text = #"Planned";
cell.myImageView.image = [UIImage imageNamed:#"Icon4.png.png"];
break;
case 4:
cell.primaryLabel.text = #"Appointment with Joey";
cell.secondaryLabel.text = #"2 Hours";
cell.myImageView.image = [UIImage imageNamed:#"Icon4.png.png"];
break;
default:
break;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
[self.navigationController pushViewController:self.teamsView animated:YES];
}
So, the problem: the data does not display in the cells and I don't understand why. Here is a screen shot of what it currently looks like.
http://dl.dropbox.com/u/1481176/Screen%20shot%202010-12-16%20at%203.30.42%20PM.png
Any help would be greatly appreciated! Thanks.

You are calling initWithStyle:reuseIdentifier: instead of your overwritten initWithFrame:reuseIdentifier:

Related

how to remove text from cell when click on the text of cell

hi i have problem when i am displaying the records of table by clicking on load more cell . problem is this when i click on load more cell then table must load two more records and then load more text must move forward for example first time 2 records load on table at the 3rd cell is the load more cell when i click on this cell then two more records must load into table and this load more text move to 5th cell . but in my case the problem is when i click on load more cell at 3rd position it also move to fifth position but on 3rd cell record not load and also load more text appear on 3rd cell. kindly tell me how i can handle this problem this is the code
#import "RootViewController.h"
#import "XMLAppDelegate.h"
#import "song.h"
#import "BookDetailViewController.h"
#implementation RootViewController
#synthesize mylabel,mylabel1,mylabel2,spinner,cell;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if([myarray count]>pageSize)
{
return pageSize+1;
}
return myarray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
song *asong = [appDelegate.artists objectAtIndex:indexPath.row];
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[myarray objectAtIndex:indexPath.row ]]];
UIImage *image=[UIImage imageWithData:data];
if(indexPath.row<pageSize)
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 60, 60)];
imageView.image=image;
[cell addSubview:imageView];
CGRect CellFrame1 = CGRectMake(65, 10, 220, 45);
mylabel1 = [[UILabel alloc] initWithFrame:CellFrame1];
mylabel1.text = asong.title;
mylabel1.font = [UIFont boldSystemFontOfSize:12];
mylabel1.textColor = [UIColor blackColor];
[cell.contentView addSubview:mylabel1];
CGRect CellFrame2 = CGRectMake(65, 47, 150, 25);
mylabel2 = [[UILabel alloc] initWithFrame:CellFrame2];
mylabel2.text = asong.artist;
mylabel2.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:mylabel2];
CGRect CellFrame = CGRectMake(255, 3, 60, 7);
mylabel = [[UILabel alloc] initWithFrame:CellFrame];
mylabel.text = asong.duration;
mylabel.textColor = [UIColor blueColor];
mylabel.font = [UIFont boldSystemFontOfSize:10];
[cell.contentView addSubview:mylabel];
}
else
{
cell.textLabel.text = #"load more";
spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner performSelector:#selector(stopAnimating) withObject:nil afterDelay:1]; [spinner release];
}
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
if (indexPath.row==pageSize)
{
pageSize=pageSize+2;
[cell reloadInputViews];
[tableView reloadData];
}
/* else if(bdvController == nil)
bdvController = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:[NSBundle mainBundle]];
song *asong = [appDelegate.artists objectAtIndex:indexPath.row];
bdvController.aartist = asong;
[self.navigationController pushViewController:bdvController animated:YES];*/
}
- (void)viewDidLoad {
[super viewDidLoad];
pageSize =2;
// Uncomment the following line to add the Edit button to the navigation bar.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
myarray = [[NSMutableArray alloc] initWithObjects:#"http://api.androidhive.info/music/images/adele.png",#"http://api.androidhive.info/music/images/eminem.png",#"http://api.androidhive.info/music/images/mj.png",#"http://api.androidhive.info/music/images/rihanna.png",#"http://api.androidhive.info/music/images/arrehman.png",#"http://api.androidhive.info/music/images/alexi_murdoch.png",#"http://api.androidhive.info/music/images/dido.png",#"http://api.androidhive.info/music/images/enrique.png",#"http://api.androidhive.info/music/images/ennio.png",#"http://api.androidhive.info/music/images/backstreet_boys.png",#"http://api.androidhive.info/music/images/adele.png",#"http://api.androidhive.info/music/images/eminem.png",#"http://api.androidhive.info/music/images/mj.png",#"http://api.androidhive.info/music/images/rihanna.png",#"http://api.androidhive.info/music/images/arrehman.png",#"http://api.androidhive.info/music/images/alexi_murdoch.png",#"http://api.androidhive.info/music/images/dido.png",#"http://api.androidhive.info/music/images/enrique.png",#"http://api.androidhive.info/music/images/ennio.png",#"http://api.androidhive.info/music/images/backstreet_boys.png",#"http://api.androidhive.info/music/images/adele.png",#"http://api.androidhive.info/music/images/eminem.png",#"http://api.androidhive.info/music/images/mj.png",#"http://api.androidhive.info/music/images/rihanna.png",#"http://api.androidhive.info/music/images/arrehman.png",#"http://api.androidhive.info/music/images/alexi_murdoch.png",#"http://api.androidhive.info/music/images/dido.png",#"http://api.androidhive.info/music/images/enrique.png",#"http://api.androidhive.info/music/images/ennio.png",#"http://api.androidhive.info/music/images/backstreet_boys.png", nil];
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
self.title = #"Songs";
}
/*
// Override to support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[bdvController release];
[appDelegate release];
[super dealloc];
}
#end
Your problem is that you're only configuring a cell if it's nil. In your example, you will want to replace cell 3's content with some song even when it's not nil.
When deque returns nil, create a new cell and add subviews but always update the content based on the path.
Try below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row<pageSize) {
song *asong = [appDelegate.artists objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 60, 60)];
imageView.tag = 1;
[cell addSubview:imageView];
CGRect CellFrame1 = CGRectMake(65, 10, 220, 45);
mylabel1 = [[UILabel alloc] initWithFrame:CellFrame1];
mylabel1.tag = 2;
mylabel1.font = [UIFont boldSystemFontOfSize:12];
mylabel1.textColor = [UIColor blackColor];
[cell.contentView addSubview:mylabel1];
CGRect CellFrame2 = CGRectMake(65, 47, 150, 25);
mylabel2 = [[UILabel alloc] initWithFrame:CellFrame2];
mylabel2.tag = 2;
mylabel2.font = [UIFont systemFontOfSize:12];
[cell.contentView addSubview:mylabel2];
CGRect CellFrame = CGRectMake(255, 3, 60, 7);
mylabel = [[UILabel alloc] initWithFrame:CellFrame];
mylabel.tag = 2;
mylabel.textColor = [UIColor blueColor];
mylabel.font = [UIFont boldSystemFontOfSize:10];
[cell.contentView addSubview:mylabel];
}
UIImageView *imageView = [cell viewWithTag:1];
UILabel *mylabel1 = [cell viewWithTag:1];
UILabel *mylabel2 = [cell viewWithTag:1];
UILabel *mylabel = [cell viewWithTag:1];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[myarray objectAtIndex:indexPath.row ]]];
dispatch_sync(dispatch_get_main_queue(), ^{
imageView.image = [UIImage imageWithData:data];
});
});
mylabel1.text = asong.title;
mylabel2.text = asong.artist;
mylabel.text = asong.duration;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
static NSString *CellIdentifier = #"cellloadmore";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = #"load more";
spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner performSelector:#selector(stopAnimating) withObject:nil afterDelay:1]; [spinner release];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

UIImageView in UITableViewCell not showing up until cell is being reused

I am having a weird issue with a custom UITableViewCell implementation that I have:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ProfileTableViewCell *cell = (ProfileTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
[cell setupCellViewWithSavedStory:myStory];
return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithPulseStyle:PNRTableViewCellAccessoryDefault reuseIdentifier:#"SavedStories"];
if (self) {
storyImageView_ = [[UIImageView alloc] initWithFrame:CGRectMake(kPadding, kPadding, kImageSize, kImageSize)];
storyImageView_.layer.borderWidth = 2.0;
storyImageView_.layer.borderColor = [UIColor whiteColor].CGColor;
storyTitleLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(storyImageView_.frameRight + kPadding*1.5, storyImageView_.frameY, self.bounds.size.width - storyImageView_.frameWidth - 4*kPadding, 0)];
storyTitleLabel_.numberOfLines = 0;
storyTitleLabel_.lineBreakMode = UILineBreakModeClip;
storyTitleLabel_.font = [UIFont fontWithName:kProximaNovaBold size:14];
storyTitleLabel_.textColor = [UIColor whiteColor];
storyTitleLabel_.backgroundColor = [UIColor clearColor];
storyPublisherLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(storyTitleLabel_.frameX, storyTitleLabel_.frameHeight, storyTitleLabel_.frameWidth, 50)];
storyPublisherLabel_.numberOfLines = 1;
storyPublisherLabel_.font = [UIFont fontWithName:kProximaNova size:11];
storyPublisherLabel_.textColor = [UIColor colorWithWhite:140/255.f alpha:1.0];
storyPublisherLabel_.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:storyImageView_];
[self.contentView addSubview:storyTitleLabel_];
[self.contentView addSubview:storyPublisherLabel_];
}
return self;
}
-(void)setupCellViewWithSavedStory:(MyStory *) myStory
{
if (myStory.imageState == StoryImageAvailableOnDisk) {
storyImageView_.hidden = NO;
storyImageView_.image = myStory.image;
}
else {
storyImageView_.image = nil;
storyImageView_.hidden = YES;
}
CGSize titleSize = [myStory.title sizeWithFont:storyTitleLabel_.font constrainedToSize:CGSizeMake(storyTitleLabel_.frameWidth, kImageSize - kPadding) lineBreakMode:storyTitleLabel_.lineBreakMode];
[storyTitleLabel_ setFrameHeight:titleSize.height];
[storyTitleLabel_ setText:savedStory.title];
[storyPublisherLabel_ setText:savedStory.domain];
[self setNeedsLayout];
}
But for some reason is that I can't see image until I scroll down and scroll back up again. In other words the image is reused then I can see the image. All of the label text is showing just fine, it's just the image. Any idea why?
Try adding this code:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
[cell setupCellViewWithSavedStory:myStory];
}
Have you tried this?
if (cell == nil) {
cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
MyStory *myStory = (SavedStory *)[fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
[cell setupCellViewWithSavedStory:myStory];
}

The first UITableViewCell's accessary arrow is not showing

Here is the code I am struggling with, the first table cell doesnt display the accessary arrow, but other table cells work fine...
Below is the code for table cell1, other cells is also customized but work fine.
- (void) initialization
{
labelTitle = [[UILabel alloc] initWithFrame:CGRectZero];
labelTitle.font = [UIFont fontForMoreLikeResultTitle];
labelTitle.textColor = [UIColor blackColor];
labelTitle.numberOfLines = 1;
labelTitle.lineBreakMode = UILineBreakModeTailTruncation;
labelTitle.backgroundColor = [UIColor clearColor];
labelFulLAddress = [[UILabel alloc] initWithFrame:CGRectZero];
labelFulLAddress.font = [UIFont fontForMoreLikeResultDescription];
labelFulLAddress.textColor = [UIColor blackColor];
labelFulLAddress.numberOfLines = 1;
labelFulLAddress.lineBreakMode = UILineBreakModeTailTruncation;
labelFulLAddress.backgroundColor = [UIColor clearColor];
[[self contentView] addSubview:labelTitle];
[[self contentView] addSubview:labelFulLAddress];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// Initialization code
[self initialization];
}
return self;
}
- (void) layoutSubviews
{
float xOffset = 20.0f;
float yOffset = 10.0f;
float currentUsedHeight = yOffset;
labelTitle.text = documentTitle;
labelTitle.frame = CGRectMake(xOffset, currentUsedHeight,
320.0f - 2 * xOffset, 60.0f);
[labelTitle sizeToFitHeight];
[labelTitle sizeToFitWidth];
labelFulLAddress.text = #"99999 Bellevue Way NE, Bellevue WA";
currentUsedHeight += (yOffset + labelTitle.frame.size.height);
labelFulLAddress.frame = CGRectMake(xOffset, currentUsedHeight, 320.0f - 2 * xOffset, 60.0f);
[labelFulLAddress sizeToFitHeight];
[labelFulLAddress sizeToFitWidth];
}
Below is the code in view controller:
- (UITableViewCell *) createResultTableCell1:(UITableView *)tableView
{
static NSString *CellIdentifier = #"FirstMoreLikeResultCell";
FirstResultTableCell *cell = (FristResultTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[MoreLikeTableCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.documentTitle = self.documentTitle;
return cell;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (indexPath.row == 0)
{
cell = [self createResultTableCell1:tableView];
}
else
{
cell = [self createResultTableCell2:tableView cellForRowAtIndexPath:indexPath];
}
return cell;
}
Call [super layoutSubviews] from within your overridden layoutSubviews.

how to add more 2 or more labels in a row of grouped table view

How can I add more than 2 labels into a row of a grouped table view?
I used the following code but it's displaying only the last label
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
switch(indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
{
lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,50, 10)] autorelease];
cell.accessoryView = lbl;
cell.textLabel.text=#"Company:";
lbl.tag = indexPath.row;
UILabel* lbl1 = [[[UILabel alloc] initWithFrame:CGRectMake(0,12,50, 10)] autorelease];
cell.accessoryView = lbl1;
cell.textLabel.text=#"Account:";
lbl1.tag = indexPath.row;
UILabel* lbl2 = [[[UILabel alloc] initWithFrame:CGRectMake(0,24,50, 10)] autorelease];
cell.accessoryView = lbl2;
cell.textLabel.text=#"Other ID:";
lbl2.tag = indexPath.row;
}
break;
case 1:
{ /*
lbl = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,50, 10)] autorelease];
cell.accessoryView = lbl;
cell.textLabel.text=#"Add New Contact:";
*/
}
break;
}
}
To increase the row height, I am using following code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch(indexPath.section)
{
case 0:
{
switch(indexPath.row)
{
case 0:
{
return 100;
}
break;
case 1:
{
return 40;
}
break;
case 2:
{
return 40;
}
break;
case 3:
{
return 40;
}
break;
default:
{
return 15;
}
}
}
break;
}
}}
you can use custom cell and can have anything in your cell, not only two labels, but any thing.....here it goes as far as your problem is concerned......
First thing we will do is create a customCell. Right click on Classes and add a new UITableViewCell subclass. Name as “CustomCell”. Now open CustomCell.h and add the following code:
#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
synthesize all the three elements in CustomCell.m as we are going to access these elements from other classes.
#synthesize primaryLabel,secondaryLabel,myImageView;
Open CustomCell.m and add the following code
- (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;
}
set the frames in layoutsubviews
- (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;
}
now go to tableViewController and just play with your labels
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #”Cell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell…
switch (indexPath.row) {
case 0:
cell.primaryLabel.text = #"Meeting on iPhone Development";
cell.secondaryLabel.text = #"Sat 10:30";
cell.myImageView.image = [UIImage imageNamed:#"meeting_color.png"];
break;
case 1:
cell.primaryLabel.text = #"Call With Client";
cell.secondaryLabel.text = #"Planned";
cell.myImageView.image = [UIImage imageNamed:#"call_color.png"];
break;
case 2:
cell.primaryLabel.text = #"Appointment with Joey";
cell.secondaryLabel.text = #"2 Hours";
cell.myImageView.image = [UIImage imageNamed:#"calendar_color.png"];
break;
case 3:
cell.primaryLabel.text = #"Call With Client";
cell.secondaryLabel.text = #"Planned";
cell.myImageView.image = [UIImage imageNamed:#"call_color.png"];
break;
case 4:
cell.primaryLabel.text = #"Appointment with Joey";
cell.secondaryLabel.text = #"2 Hours";
cell.myImageView.image = [UIImage imageNamed:#"calendar_color.png"];
break;
default:
break;
}
return cell;
}
of course, change the image name//,.......regards

How to pass a value from the TableView controller to a customCell class

I'm creating a custom UITableViewCell programmatically, which uses the following code. What im wondering is how to make the background of it Transparent?
Reason why I want it transparent is that the UITableViewCell's height is dynamic and i cant figure out how to pass the "dynamic height" code to the customCell class, so i figured i'd set a max height of 300 or so for the CGRect and make the background transparent so it doesn't overlap the following cells.
Making the label transparent doesn't work, so I need to pass the TableViewCell height to the custom class.
customCell class
#import "customCell.h"
#implementation customCell
#synthesize primaryLabel;
#synthesize secondaryLabel;
#synthesize priceLabel;
#synthesize rectColor;
- (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:10];
secondaryLabel = [[UILabel alloc]init];
secondaryLabel.textAlignment = UITextAlignmentLeft;
secondaryLabel.font = [UIFont systemFontOfSize:8];
priceLabel = [[UILabel alloc]init];
priceLabel.textAlignment = UITextAlignmentLeft;
priceLabel.font = [UIFont systemFontOfSize:10];
//self.rectColor = kDefaultRectColor;
[self.contentView addSubview:primaryLabel];
[self.contentView addSubview:secondaryLabel];
[self.contentView addSubview:priceLabel];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
//CGContextSetFillColor(context, [UIColor clearColor]);
frame= CGRectMake(boundsX+5 ,5, 200, 15);
primaryLabel.frame = frame;
frame= CGRectMake(boundsX+10 ,20, 300, 15);
secondaryLabel.frame = frame;
frame= CGRectMake(boundsX+270 ,0, 50, 15);
priceLabel.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
UITableView Class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell = [[[customCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.primaryLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.primaryLabel.font = [UIFont boldSystemFontOfSize:18];
cell.primaryLabel.numberOfLines = ceilf([[[data objectAtIndex:indexPath.row] objectForKey:#"description"] sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
cell.secondaryLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:#"description"];
cell.secondaryLabel.font = [UIFont systemFontOfSize:14];
cell.secondaryLabel.numberOfLines = ceilf([[[data objectAtIndex:indexPath.row] objectForKey:#"description"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
cell.priceLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:#"price"];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *titleString = [[data objectAtIndex:indexPath.row] objectForKey:#"title"];
NSString *detailString = [[data objectAtIndex:indexPath.row] objectForKey:#"description"];
CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGSize detailSize = [detailString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return detailSize.height+titleSize.height;
}
If you make your table cells massive and transparent you're going to make the rendering fantastically slow!
I'd take a look at the delegate protocol - this method is how you tell a cell how high it should be.
Calling [tabelView reloadData] will cause this method to be called for each cell. Put your dynamic height code in there.
Hope that helps.
set the backgroundColor on the cell to be "[UIColor clearColor]"
Because your row height is dependent on your custom cell layout (based on labels), I would put the sizing logic inside the custom cell class (in layoutSubviews) and in your tableViewController do the following:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// initialize and configure your cell
[cell setNeedsLayout];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.bounds.size.height;
}
And I agree with the others that transparent cells should be avoided if possible.