adding subviews to cell view showing strange behavior - iphone

I m trying to add cells to a tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
// Set up the cell
cell.backgroundColor = [UIColor clearColor];
UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImageView *img;
UILabel *lbl;
UIImageView *backImage;
UILabel *textLabel;
UILabel *detailTextLabel;
NSInteger val = [indexPath row] * 3;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//*buyBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
[buyBtn setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
//*img = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
[img setImage:[UIImage imageNamed:#""]];
//*lbl = [[UILabel alloc] initWithFrame:CGRectMake(x,y,w,h)];
}
else
{
backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)];
[buyBtn setFrame:CGRectMake(257, 100, 57, 24)];
img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 75, 57, 24)];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 57, 24)];
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 230, 25)];
detailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 25, 230, 30)];
backImage.image = [UIImage imageNamed:#"shop-bg.png"];
[buyBtn setImage:[UIImage imageNamed:#"buy_button"] forState:UIControlStateNormal];
[img setImage:[UIImage imageNamed:#"price_button.png"]];
lbl.center = img.center;
lbl.textAlignment = UITextAlignmentCenter;
lbl.text = [self.original_List objectAtIndex:val+2];
lbl.backgroundColor = [UIColor clearColor];
textLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:14];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.text = [self.original_List objectAtIndex:val];
detailTextLabel.text = [self.original_List objectAtIndex:val+1];
detailTextLabel.backgroundColor = [UIColor clearColor];
detailTextLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:10];
textLabel.textColor = [UIColor blackColor];
detailTextLabel.textColor = [UIColor whiteColor];
detailTextLabel.numberOfLines = 2;
}
[buyBtn setTag:[indexPath row]];
[buyBtn addTarget:self action:#selector(buyBtnPressed:) forControlEvents:UIControlEventTouchDown];
[cell addSubview:backImage];
[cell addSubview:detailTextLabel];
[cell addSubview:textLabel];
[cell addSubview:buyBtn];
[cell addSubview:img];
[cell addSubview:lbl];
return cell;
}
From figure it can be seen that The first Cell DOES NOT Contain the added image, label and button, however if other cells are scrolled those who lost view turn transparent while those who are in view are okay.
Why is my three subview not getting added from first cell, my textLabel and detailTextLabel are added as they should.

First of all you don't dequeue the cell and always create a new one without reusing. Try adding something like this (not sure if you need autorelease, depends on weather you use ARC or not):
RecentsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[RecentsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
}
Second, if you are not using ARC (auto ref counting) then you have bunch of lost refs here.
[[Class alloc] init] adds the ref and addSubview also adds the ref. I'm wondering what xcode "analyse" build says on this.
Third, imho all labels you added to the cell need also their "frame" property to set up, not just number of lines, etc.

here is example of how I did it. I created cell class derived what what you need (CopyableCell in my case).
This is *.h file.
#import <UIKit/UIKit.h>
#import "CopyableCell.h"
#interface RecentsTableViewCell : CopyableCell
#property (readonly, retain) UILabel *titleLabel;
#property (readonly, retain) UILabel *detailsLabel;
#property (readonly, retain) UILabel *subdetailsLabel;
#property (readonly, retain) UILabel *subtitleLabel;
#end
Here is *.m file.
#import "RecentsTableViewCell.h"
#interface RecentsTableViewCell ()
// Redeclare property as readwrite
#property (readwrite, retain) UILabel *titleLabel;
#property (readwrite, retain) UILabel *subtitleLabel;
#property (readwrite, retain) UILabel *detailsLabel;
#property (readwrite, retain) UILabel *subdetailsLabel;
#end
#implementation RecentsTableViewCell
#synthesize titleLabel;
#synthesize subtitleLabel;
#synthesize detailsLabel;
#synthesize subdetailsLabel;
- (void) layoutSubviews
{
int leftMargin = 30;
int rightMargin = 6;
[super layoutSubviews];
CGRect cellRect = self.contentView.frame;
int width = cellRect.size.width - (leftMargin + rightMargin);
CGSize constraintSize = CGSizeMake(300, MAXFLOAT);
CGSize titleSize = [titleLabel.text sizeWithFont:titleLabel.font
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeTailTruncation];
CGSize detailsSize = [detailsLabel.text sizeWithFont:detailsLabel.font
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeTailTruncation];
if (titleSize.width > titleLabel.frame.size.width ||
detailsSize.width > detailsLabel.frame.size.width) {
CGRect detailsFrame = detailsLabel.frame;
detailsFrame.size.width = detailsSize.width;
detailsFrame.origin.x = cellRect.size.width -
(detailsSize.width + subdetailsLabel.frame.size.width + rightMargin);
CGRect titleFrame = titleLabel.frame;
titleFrame.size.width = width - detailsSize.width - subdetailsLabel.frame.size.width - 5;
titleLabel.frame = titleFrame;
detailsLabel.frame = detailsFrame;
}
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
CGRect cellRect = self.contentView.frame;
cellRect.size.height = 61;
self.contentView.frame = cellRect;
int leftMargin = 30;
int rightMargin = 6;
int x = cellRect.origin.x + leftMargin;
int y = cellRect.origin.y;
int width = cellRect.size.width - (leftMargin + rightMargin);
int height = cellRect.size.height;
int titleWidth = width * 0.75;
int alldetailsWidth = width - titleWidth;
int detailsWidth = alldetailsWidth * 0.65;
int subdetailsWidth = alldetailsWidth - detailsWidth;
int titleHeight = height * 0.38;
int subtitleHeight = height - titleHeight;
self.titleLabel = [[[UILabel alloc]
initWithFrame:CGRectMake(x, y, titleWidth, titleHeight)] autorelease];
self.titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
self.titleLabel.highlightedTextColor = self.textLabel.highlightedTextColor;
self.titleLabel.textAlignment = UITextAlignmentLeft;
self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleBottomMargin;
[self.contentView addSubview:titleLabel];
self.detailsLabel = [[[UILabel alloc]
initWithFrame:CGRectMake(cellRect.size.width -
(detailsWidth + subdetailsWidth + rightMargin),
y + 4, detailsWidth, titleHeight - 4)] autorelease];
self.detailsLabel.font = [UIFont boldSystemFontOfSize:13.0f];
self.detailsLabel.textColor = [UIColor colorWithRed:0.12 green:0.439 blue:0.847 alpha:1];
self.detailsLabel.textAlignment = UITextAlignmentRight;
self.detailsLabel.highlightedTextColor = self.textLabel.highlightedTextColor;
self.detailsLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:detailsLabel];
self.subdetailsLabel = [[[UILabel alloc]
initWithFrame:CGRectMake(cellRect.size.width - (subdetailsWidth + rightMargin),
y + 6, subdetailsWidth, titleHeight - 6)] autorelease];
self.subdetailsLabel.font = [UIFont systemFontOfSize:11.0f];
self.subdetailsLabel.textColor = [UIColor colorWithRed:0.12 green:0.439 blue:0.847 alpha:1];
self.subdetailsLabel.textAlignment = UITextAlignmentCenter;
self.subdetailsLabel.highlightedTextColor = self.textLabel.highlightedTextColor;
self.subdetailsLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:subdetailsLabel];
self.subtitleLabel = [[[UILabel alloc]
initWithFrame:CGRectMake(x, y + titleHeight, width, subtitleHeight - 2)] autorelease];
self.subtitleLabel.highlightedTextColor = self.textLabel.highlightedTextColor;
self.subtitleLabel.font = [UIFont systemFontOfSize:13.0f];
self.subtitleLabel.textColor = [UIColor darkGrayColor];
self.subtitleLabel.numberOfLines = 2;
self.subtitleLabel.lineBreakMode = UILineBreakModeTailTruncation;
self.subtitleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleTopMargin;
[self.contentView addSubview:subtitleLabel];
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return self;
}
- (id) init
{
return [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"RecentsCell"];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
- (void) dealloc
{
self.titleLabel = nil;
self.subtitleLabel = nil;
self.detailsLabel = nil;
self.subdetailsLabel = nil;
[super dealloc];
}
#end
Note that I'm not using ARC so if you use this code, modify it accordingly. Here is how I use this class in my table view controller.
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"TransactionCell";
NSMutableArray *data = searching ? searchResult : dataSource;
NSDictionary *object = [data objectAtIndex:[indexPath row]];
RecentsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[RecentsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.indexPath = indexPath;
cell.delegate = self;
cell.titleLabel.text = #"Transaction title";
if (pendingTransaction) {
cell.titleLabel.textColor = [UIColor grayColor];
cell.titleLabel.font = [UIFont systemFontOfSize:cell.titleLabel.font.pointSize];
} else {
if (!isGreen)
cell.titleLabel.textColor = [UIColor colorWithRed:0.733 green:0.098
blue:0.098 alpha:1];
else
cell.titleLabel.textColor = cell.textLabel.textColor;
cell.titleLabel.font = [UIFont boldSystemFontOfSize:cell.titleLabel.font.pointSize];
}
cell.subtitleLabel.text = #"Transaction subtitle";
cell.detailsLabel.text = #"Some details like location";
cell.subdetailsLabel.text = #"200 USD";
return cell;
}
This produces table very similar to what you see in iphone call app, that is, title, subtitle, subdetails in blue at rightmost position of the cell, etc.
You can see it in Torchoo app in the appstore. It is free to use and you can use test account to log in and see it as example.
Hope this helps a bit.

Are you continuously adding subView to cell not removing any subView?
Check condition while adding subView like code not exact I have written:
if ([[cell.contentView subviews] count]==0) {
[self.contentView addSubview:imageView];
}
If cell content View doesn't contain that View then add it.

Previous answer is correct although there is more that you could fix.
The part of code that adds subviews should only be called in case when the cell is not dequeued. Otherwise you'll be adding multiple instances of the same buttons, views etc.
[cell addSubview:backImage];
[cell addSubview:detailTextLabel];
[cell addSubview:textLabel];
[cell addSubview:buyBtn];
[cell addSubview:img];
[cell addSubview:lbl];
Also I guess it is better to add to cell.contentView instead of directly to cell. So it should look something like this:
RecentsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[RecentsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
/*Create your subviews here*/
[cell.contentView addSubview:backImage];
[cell.contentView addSubview:detailTextLabel];
[cell.contentView addSubview:textLabel];
[cell.contentView addSubview:buyBtn];
[cell.contentView addSubview:img];
[cell.contentView addSubview:lbl];
}
// Below just set properties of existing subviews of the cell: color, textColor, text etc.
// You need to retrieve them and one way is to assign them to properties in
// RecentsTableViewCell or set the tag property of the subviews and retrieve
// them using e.g. [cell.contentView viewWithTag:kBuyButtonTag].
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
...
}
else {
...
}

Related

Custom UITableViewCell is empty, although cell from heightForRowAtIndexPath is correct

Sorry, I am missing it. I have a beautiful table with correct (variable) row heights. But all the cells are blank. Three labels should be populated in each cell.
UPDATE: FIX IS BELOW
#implementation MasterTableCell
#synthesize labelDesc;
#synthesize labelDuration;
#synthesize labelName;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// name
CGRect frameTextLabel = CGRectMake(8, 2, 244, 25);
labelName = [[UILabel alloc] initWithFrame:frameTextLabel];
labelName.lineBreakMode = UILineBreakModeTailTruncation;
labelName.font = [UIFont boldSystemFontOfSize:17.0];
labelName.textColor = [UIColor blackColor];
// description
labelDesc = [[UILabel alloc] init];
labelDesc.lineBreakMode = UILineBreakModeWordWrap;
labelDesc.numberOfLines = 0;
labelDesc.textColor = [UIColor grayColor];
labelDesc.font = [UIFont systemFontOfSize:14.0f];
labelDesc.backgroundColor = [UIColor blueColor];
// duration
CGRect frame = CGRectMake(252, 5, 40, 20);
labelDuration = [[UILabel alloc] initWithFrame:frame];
labelDuration.font = [UIFont boldSystemFontOfSize:14.f ];
labelDuration.textAlignment = UITextAlignmentRight;
labelDuration.backgroundColor = [UIColor redColor]; // to see it
[self.contentView addSubview:labelName];
[self.contentView addSubview:labelDesc];
[self.contentView addSubview:labelDuration];
}
return self;
}
#end
And
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RecipeCell";
MasterTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[MasterTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Recipe *recipeAtIndex = [sortedArray objectAtIndex:indexPath.row];
cell.labelName.text = #"Test1";
cell.labelDesc.text = #"Test2";
cell.labelDuration.text = [TBCommon formattedStringforDuration:recipeAtIndex.duration withDelimeter:#":"];
CGRect frameDescLabel = CGRectMake(8, 25, 284, [self heightForDescriptionFrame:recipeAtIndex.description]);
cell.labelDesc.frame = frameDescLabel;
return cell;
}
FIX
#import "MasterTableCell.h"
#implementation MasterTableCell : UITableViewCell
#synthesize labelDesc;
#synthesize labelDuration;
#synthesize labelName;
- (void)awakeFromNib
{
[super awakeFromNib];
// name
CGRect frameTextLabel = CGRectMake(8, 2, 244, 25);
labelName = [[UILabel alloc] initWithFrame:frameTextLabel];
labelName.lineBreakMode = UILineBreakModeTailTruncation;
labelName.font = [UIFont boldSystemFontOfSize:17.0];
labelName.textColor = [UIColor blackColor];
// description
labelDesc = [[UILabel alloc] init];
labelDesc.lineBreakMode = UILineBreakModeWordWrap;
labelDesc.numberOfLines = 0;
labelDesc.textColor = [UIColor grayColor];
labelDesc.font = [UIFont systemFontOfSize:14.0f];
// duration
CGRect frame = CGRectMake(252, 5, 40, 20);
labelDuration = [[UILabel alloc] initWithFrame:frame];
labelDuration.textColor = [UIColor colorWithRed:38.0/255.0 green:111.0/255.0 blue:208.0/255.0 alpha:1];
labelDuration.font = [UIFont boldSystemFontOfSize:14.f ];
labelDuration.textAlignment = UITextAlignmentRight;
[self.contentView addSubview:labelName];
[self.contentView addSubview:labelDesc];
[self.contentView addSubview:labelDuration];
}
#end
And
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MasterTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Recipe *recipeAtIndex = [sortedArray objectAtIndex:indexPath.row];
cell.labelName.text = recipeAtIndex.name;
cell.labelDesc.text = recipeAtIndex.description;
cell.labelDuration.text = [TBCommon formattedStringforDuration:recipeAtIndex.duration withDelimeter:#":"];
CGRect frameDescLabel = CGRectMake(8, 25, 284, [self heightForDescriptionFrame:recipeAtIndex.description]);
cell.labelDesc.frame = frameDescLabel;
return cell;
}
If you're using Storyboards, initWithStyle will never be called. Move the label creation code into awakeFromNib.
You can get rid of the whole if (cell == nil) part too because dequeueReusableCell will ALWAYS return a cell.
Do you think that your table view delegate/datasource method are actually being called. Trying putting a break point in cellForRowAtIndexPath method to verify that. If breakpoint's not hit it means your delegate and datasource are not set. Set those.
Another thing that I noticed is that you have not set the frame for your labelDesc instance variable. That's worth looking at.
When I was setting up my custom cell subclasses, the tutorial I was following had me set the frames in a separate method called layoutSubviews. For you, it would look like
- (void) layoutSubviews
{
[super layoutSubviews];
labelName.frame = CGRectMake(8, 2, 244, 25);
labelDuration.frame = CGRectMake(252, 5, 40, 20);
}
When I had something to add to a cell that is dynamically located like labelDesc is, I had to totally pull it out of my custom cells class and put it all in cellForRowAtIndexPath (so I would have something like labelDesc = [[UILabel alloc] initWithFrame... and so on all in cellForRowAtIndexPath, and nothing for it in the initWithStyle).
I don't know why doing it this way is any different than setting the frames in initWithStyle, but it seems to be working for me.

UIButton with UIImages in a UITableViewCell - Overlapping etc

I have a problem with my UITableViewCell's. I am developing an application where certain posts in a feed may contain an image, and the button clicked will also need to contain a 'tag' number depending on which row of the table it is as I have an array of images downloaded.
The problem lies when I am scrolling (as you have already guessed) the table. The image loads fine, however when I scroll up/down the image will then go above other cells, and when the cell is shown back on the screen, the image will quickly change it's position to the proper position etc.
Here's the code for my UITableView:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [feed count];
}
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier
{
CGRect rect = [self.tableView bounds];
CGRect CellFrame = CGRectMake(0.0f, 0.0f, rect.size.width, 10.0f);
CGRect TitleFrame = CGRectMake(55.0f, 10.0f, 240.0f, 15.0f);
CGRect TextFrame = CGRectMake(55.0f, 25.0f, 250.0f, 15.0f);
CGRect PinFrame = CGRectMake(21.0f, 10.0f, 30.0f, 30.0f);
CGRect ViewFrame = CGRectMake(50.0f, 35.0f, 260.0f, 0.0f);
CGRect CommentsFrame = CGRectMake(300.0f, 10.0f, 20.0f, 15.0f);
UILabel * lblTemp;
UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
[cell setFrame:CellFrame];
lblTemp = [[UILabel alloc] initWithFrame:TitleFrame];
lblTemp.tag = 1;
lblTemp.numberOfLines = 0;
lblTemp.opaque = NO;
lblTemp.font = [UIFont systemFontOfSize:13.0f];
lblTemp.backgroundColor = [UIColor clearColor];
lblTemp.textColor = [UIColor blackColor];
lblTemp.textAlignment = UITextAlignmentLeft;
lblTemp.shadowColor = [UIColor whiteColor];
lblTemp.shadowOffset = CGSizeMake(0, 1);
[cell.contentView addSubview:lblTemp];
lblTemp = [[UILabel alloc] initWithFrame:TextFrame];
lblTemp.tag = 2;
lblTemp.numberOfLines = 0;
lblTemp.lineBreakMode = UILineBreakModeWordWrap;
lblTemp.adjustsFontSizeToFitWidth = NO;
lblTemp.font = [UIFont systemFontOfSize:13.0f];
lblTemp.textColor = [UIColor lightGrayColor];
lblTemp.backgroundColor = [UIColor clearColor];
lblTemp.shadowColor = [UIColor whiteColor];
lblTemp.shadowOffset = CGSizeMake(0, 1);
[cell.contentView addSubview:lblTemp];
lblTemp = [[UILabel alloc] initWithFrame:CommentsFrame];
lblTemp.tag = 5;
lblTemp.numberOfLines = 1;
lblTemp.font = [UIFont systemFontOfSize:13.0f];
lblTemp.textColor = [UIColor lightGrayColor];
lblTemp.backgroundColor = [UIColor whiteColor];
lblTemp.shadowColor = [UIColor whiteColor];
lblTemp.shadowOffset = CGSizeMake(0, 1);
[cell.contentView addSubview:lblTemp];
UIImageView * imgTemp = [[UIImageView alloc] initWithFrame:PinFrame];
imgTemp.tag = 3;
[cell.contentView addSubview:imgTemp];
UIView * viewTemp = [[UIView alloc] initWithFrame:ViewFrame];
viewTemp.tag = 4;
[viewTemp setBackgroundColor:[UIColor whiteColor]];
[cell.contentView addSubview:viewTemp];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = #"Cell";
UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//UITableViewCell * cell = [self getCellContentView:CellIdentifier];
if (cell == nil) {
cell = [self getCellContentView:CellIdentifier];
} else {
[cell.contentView viewWithTag:999];
}
cell.backgroundColor = [UIColor clearColor];
UILabel * title = (UILabel *)[cell viewWithTag:1];
UILabel * journey = (UILabel *)[cell viewWithTag:2];
UIImageView * pin = (UIImageView *) [cell viewWithTag:3];
UILabel * comments = (UILabel *)[cell viewWithTag:5];
title.text = [NSString stringWithFormat:#"%#", [[feed objectAtIndex:indexPath.row] objectForKey:#"comment"]];
[title sizeToFit];
title.frame = CGRectMake(title.frame.origin.x, title.frame.origin.y, 240, title.frame.size.height);
if ([[feed objectAtIndex:indexPath.row] objectForKey:#"weather_desc"] != [NSNull null] ||
[[feed objectAtIndex:indexPath.row] objectForKey:#"weather_temp"] != [NSNull null]) {
journey.text = [NSString stringWithFormat:#"It's %# and %#°C.", [[feed objectAtIndex:indexPath.row] objectForKey:#"weather_desc"], [[feed objectAtIndex:indexPath.row] objectForKey:#"weather_temp"]];
} else {
journey.text = [NSString stringWithFormat:#"%#", [[[feed objectAtIndex:indexPath.row] objectForKey:#"journey"] objectForKey:#"name"]];
}
journey.frame = CGRectMake(journey.frame.origin.x, 10 + title.frame.size.height, 240, 15);
if ([[feed objectAtIndex:indexPath.row] objectForKey:#"latitude"] != [NSNull null]) {
pin.image = [UIImage imageNamed:#"LocationPin.png"];
} else {
pin.image = [UIImage imageNamed:#"Pin.png"];
}
if ([[feed objectAtIndex:indexPath.row] objectForKey:#"image_file_size"] != [NSNull null]) {
NSString * text = [NSString stringWithFormat:#"%#", [[feed objectAtIndex:indexPath.row] objectForKey:#"comment"]];
CGFloat height = [text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(250, 1500) lineBreakMode:UILineBreakModeWordWrap].height;
UIImage * myImage = [images objectForKey:[NSString stringWithFormat:#"%i", indexPath.row]];
UIButton * button = [[UIButton alloc] initWithFrame:CGRectZero];
[button setBackgroundImage:myImage forState:UIControlStateNormal];
[button setFrame:CGRectMake(title.frame.origin.x + 55.0f, 40.0f + height, 250.0f, myImage.size.height)];
[button setTag:10 + indexPath.row];
[button addTarget:self action:#selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
UIView * backView = (UIView *) [cell viewWithTag:4];
[backView setFrame:CGRectMake(title.frame.origin.x + 50.0f, 35.0f + height, 260.0f, myImage.size.height + 10.0f)];
} else {
UIView * backView = (UIView *) [cell viewWithTag:4];
[backView setFrame:CGRectZero];
[backView setBackgroundColor:[UIColor clearColor]];
}
comments.text = [NSString stringWithFormat:#"%i", [[[feed objectAtIndex:indexPath.row] objectForKey:#"comments"] count]];
UIImage * background = [UIImage imageNamed:#"CellBackground.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:background];
imageView.contentMode = UIViewContentModeScaleToFill;
[cell setBackgroundView:imageView];
return cell;
}
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
MilestoneView * milestoneView = [[MilestoneView alloc] initWithNibName:#"MilestoneView" bundle:nil];
[milestoneView setMilestone:[feed objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:milestoneView animated:YES];
}
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * text = [NSString stringWithFormat:#"%#", [[feed objectAtIndex:indexPath.row] objectForKey:#"comment"]];
CGFloat height = [text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(240, 1500) lineBreakMode:UILineBreakModeWordWrap].height;
if ([[feed objectAtIndex:indexPath.row] objectForKey:#"image_file_size"] != [NSNull null]) {
UIImage * myImage = [images objectForKey:[NSString stringWithFormat:#"%i", indexPath.row]];
height += myImage.size.height + 15.0f;
}
return height + 37;
}
I am also using Apple's inbuilt 'ARC' with iOS 5.0.
Kind regards,
Dominic
In this code you add button in cellForRowAtIndexPath , hence this is happen....
so , you add the code for button , backView in getCellContentView and fetch images from array in getCellContentView.
UIImage *myImage = [images objectForKey:[NSString stringWithFormat:#"%i", indexPath.row]];
then,
UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
[button setBackgroundImage:myImage forState:UIControlStateNormal];
[button setFrame:CGRectMake(55.0f, 40.0f, 250.0f, myImage.size.height)];
change height , width & x,y co-or. what u want and
set cell index to button.
[button setTag:indexPath.row];
[button addTarget:self action:#selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
Let me know if you have any problem....
Replace following function header
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
.
.
}
with this function
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier indexPathForButtonTag:(NSIndexPath*)indexPath {
.
.
}
Replace following line in cellForRowAtIndex
cell = [self getCellContentView:CellIdentifier];
With the
cell = [self getCellContentView:CellIdentifier indexPathForButtonTag:indexPath];
You dont need each button to have a different tag. You should not be adding any subviews to the cell in cellForRowAtIndexPath because you will inevitably end up adding subview on top of subview. I notice you are also adding an image view as the cell's background in this method - you should only do this when you are creating a new cell.
Im going to assume there is a good reason you are not using a xib file for this cell layout, so just add the button in your contentView method and give it a known tag.
In your button's action method you can find out which row of the table the button was in by using the locationInView: and indexPathForRowAtPoint: methods:
CGPoint hit = [sender locationInView:self.tableView];
NSIndexPath *hitRow = [self.tableView indexPathForRowAtPoint:hit];
Also, note that setting a view's .hidden property to YES is probably less effort that setting its frame to CGRectZero.
Ended up fixing the solution by doing the following simple code:
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview]superview]];
I used the UIButton sender to get the cell that it was within. I also pulled out all the modifications of the .tag's and kept them at the same number.

Programmatically created UITableViewCell subclass only working on highlight

I've created a subclass of UITableViewCell but I'm struggling to get it to work properly. If I use UITableViewStyleDefault then the class only works when highlighted. If I use UITableViewStyleValue1 then it mostly works but I'm unable to change label fonts much.
I tried researching but it seems everyone is doing this via a .xib file, but not programmatically.
Implementation file
#import "ASCustomCellWithCount.h"
#implementation ASCustomCellWithCount
#synthesize primaryLabel,secondaryLabel,contentCountImage,contentCount;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
contentCountImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"tableCount.png"] ];
primaryLabel = [[UILabel alloc] init];
primaryLabel.textAlignment = UITextAlignmentLeft;
primaryLabel.textColor = [UIColor blackColor];
primaryLabel.font = [UIFont systemFontOfSize: 20];
primaryLabel.backgroundColor = [UIColor clearColor];
secondaryLabel = [[UILabel alloc] init];
secondaryLabel.textAlignment = UITextAlignmentLeft;
secondaryLabel.textColor = [UIColor blackColor];
secondaryLabel.font = [UIFont systemFontOfSize: 8];
secondaryLabel.backgroundColor = [UIColor clearColor];
contentCount = [[UILabel alloc] init];
contentCount.textAlignment = UITextAlignmentCenter;
contentCount.font = [UIFont boldSystemFontOfSize: 15];
contentCount.textColor = [UIColor whiteColor];
contentCount.shadowColor = [UIColor blackColor];
contentCount.shadowOffset = CGSizeMake(1, 1);
contentCount.backgroundColor = [UIColor clearColor];
[self.contentView addSubview: contentCountImage];
[self.contentView addSubview: primaryLabel];
[self.contentView addSubview: secondaryLabel];
[self.contentView addSubview: contentCount];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
// CGFloat boundsX = contentRect.origin.x;
primaryLabel.frame = CGRectMake(0 ,0, 200, 25);
secondaryLabel.frame = CGRectMake(0, 30, 100, 15);
contentCount.frame = CGRectMake(contentRect.size.width - 48, contentRect.size.height / 2 - 13, 36, 24);
contentCountImage.frame = CGRectMake(contentRect.size.width - 48, contentRect.size.height / 2 - 12, 36, 24);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[primaryLabel release];
[secondaryLabel release];
[contentCountImage release];
[contentCount release];
}
#end
And then to create the cell I use
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
ASCustomCellWithCount *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[ASCustomCellWithCount alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:#"%#", [tempArray objectAtIndex: indexPath.row]];
cell.contentCount.text = #"49";
return cell;
}
For the sake of completeness, my issue was trying to use the custom cells default cell.textLabel.text rather than my custom cell.primaryLabel.text.
As soon as you use one of the default objects in a cell it reverts to the default cells rather than a subclass.

UITableView with performance issues

I make an UITableview (with IB) in my application with 3 types of cell. All works fine except when i drag my tableview all become very slow. So if someone can help me to improve the performance of my app it'll be cool.
My Code for the tableview :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[myData objectForKey:#"listnews"] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 154;
}
if (indexPath.row == 1) {
return 34;
}
else return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[myTextField resignFirstResponder];
ListNewsCell *cCell = (ListNewsCell *)[myTable dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"cCellIdentifier%d",indexPath.row]];
cCell = [[ListNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:#"cCellIdentifier%d",indexPath.row]];
if (indexPath.row == 0) {
NSString * caroline = [[[myData objectForKey:#"listnews"] objectAtIndex:indexPath.row] objectForKey:#"urlimage"];
cCell.c1Image1.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",caroline]]]];
cCell.c1Image2.image = [UIImage imageNamed:#"barreNoireHub.png"];
NSString * sophie = [[[myData objectForKey:#"listnews"] objectAtIndex:indexPath.row] objectForKey:#"titre"];
NSString *itemTitle = sophie ? [sophie stringByConvertingHTMLToPlainText] : #"[No Title]";
cCell.c1Label1.text = itemTitle;
NSString * sonia = [[[myData objectForKey:#"listnews"] objectAtIndex:indexPath.row] objectForKey:#"extrait"];
NSString *itemExtrait = sonia ? [sonia stringByConvertingHTMLToPlainText] : #"[No Title]";
cCell.c1Label2.text = itemExtrait;
}
if (indexPath.row == 1) {
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 34)];
imageView.backgroundColor = [UIColor whiteColor];
imageView.image = [UIImage imageNamed:#"barreSearchHub.png"];
[cCell addSubview:imageView];
[imageView release];
UIView * insertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 34)];
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 6, 270, 25)];
myTextField.placeholder = #"Recherche";
myTextField.tag = 1;
myTextField.delegate = self;
myTextField.returnKeyType = UIReturnKeySearch;
[insertView addSubview:myTextField];
cCell.accessoryView = insertView;
}
if (indexPath.row > 1) {
cCell.c2Image1.image = [UIImage imageNamed:#"cellM.png"];
NSString * caroline = [[[myData objectForKey:#"listnews"] objectAtIndex:indexPath.row] objectForKey:#"urlimage"];
cCell.c2Image2.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",caroline]]]];
NSString * sophie = [[[myData objectForKey:#"listnews"] objectAtIndex:indexPath.row] objectForKey:#"titre"];
NSString *itemTitle = sophie ? [sophie stringByConvertingHTMLToPlainText] : #"[No Title]";
cCell.c2Label1.text = itemTitle;
NSString * sonia = [[[myData objectForKey:#"listnews"] objectAtIndex:indexPath.row] objectForKey:#"extrait"];
NSString *itemExtrait = sonia ? [sonia stringByConvertingHTMLToPlainText] : #"[No Title]";
cCell.c2Label2.text = itemExtrait;
}
return cCell;
}
The code of the UITableViewCell:
#import "ListNewsCell.h"
#implementation ListNewsCell
#synthesize c1Image1, c1Image2, c1Label1, c1Label2;
#synthesize c2Image1, c2Image2, c2Label1, c2Label2;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
c1Image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,154)];
c1Image1.backgroundColor = [UIColor clearColor];
[self addSubview:c1Image1];
[c1Image1 release];
c1Image2 = [[UIImageView alloc] initWithFrame:CGRectMake(0,98,320,56)];
c1Image2.backgroundColor = [UIColor clearColor];
[self addSubview:c1Image2];
[c1Image2 release];
c1Label1 = [[UILabel alloc] initWithFrame:CGRectMake(5, 100, 310, 20)];
c1Label1.font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
c1Label1.textColor = [UIColor whiteColor];
c1Label1.backgroundColor = [UIColor clearColor];
[self addSubview:c1Label1];
c1Label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 112, 310, 45)];
c1Label2.font = [UIFont fontWithName:#"Arial" size:12];
c1Label2.textColor = [UIColor whiteColor];
c1Label2.numberOfLines = 2;
c1Label2.backgroundColor = [UIColor clearColor];
[self addSubview:c1Label2];
c2Image1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,70)];
c2Image1.backgroundColor = [UIColor clearColor];
[self addSubview:c2Image1];
[c2Image1 release];
c2Label1 = [[UILabel alloc] initWithFrame:CGRectMake(105, 8, 180, 20)];
c2Label1.font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
c2Label1.textColor = [UIColor blackColor];
c2Label1.backgroundColor = [UIColor clearColor];
[self addSubview:c2Label1];
c2Label2 = [[UILabel alloc] initWithFrame:CGRectMake(105, 25, 180, 45)];
c2Label2.font = [UIFont fontWithName:#"Arial" size:12];
c2Label2.textColor = [UIColor blackColor];
c2Label2.numberOfLines = 2;
c2Label2.backgroundColor = [UIColor clearColor];
[self addSubview:c2Label2];
c2Image2 = [[UIImageView alloc] initWithFrame:CGRectMake(20,10,75,55)];
c2Image2.backgroundColor = [UIColor clearColor];
[self addSubview:c2Image2];
[c2Image2 release];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
- (void)dealloc
{
[super dealloc];
}
#end
Thanks to all !
The following line causes the big delay:
cCell.c2Image2.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",caroline]]]];
Everytime one of these cells will be loaded, it will make a URL Request. Try to preload a bunch of the data instead of requesting it per cell.
Load the image once, make it a class variable, then just reuse its pointer like:
cCell.c2Image2.image = preloadedImage;
Declare in your .h file:
UIImage * preloadedImage;
And in the - (void) loadView -method put:
preloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",caroline]]]];
Also mind that as jrturton wrote, you want to reuse your dequeued cells for the extra performance boost.
You are never reusing your dequeued cells, so you are recreating everything from scratch every time. Since you have a unique cell for each row (is this really necessary for cells in row 2 and above?) you only need to create and populate them once.
So, you could wrap everything after dequeueReusableCellWithIdentifier onwards inside if (cCell == nil) (the return statement excepted, obviously).
In cellForRowAtIndexPath method you have dequeued some cells from the table view. But you have never used that value. That caused the problem of loading every time rather than re-using.
Try the following code in which I've modified a little..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[myTextField resignFirstResponder];
ListNewsCell *cCell = (ListNewsCell *)[myTable dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"cCellIdentifier%d",indexPath.row]];
if(cCell == nil)
{
cCell = [[ListNewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:#"cCellIdentifier%d",indexPath.row]];
}
if (indexPath.row == 0) {
// Some code here
}
if (indexPath.row == 1) {
//Some code here
}
if (indexPath.row > 1) {
// some code here
}
return cCell;
}
Thanks,
Arun.

how to add more than one image and button in UITableView?

I want to add more than one image in table view.
I can add one image using cell.imageview.image.
but how to add one more image.
And I also want to add buttons in all cells.
How can I do that ?
You should make your own UITableViewCell subclass. There are many tutorials for that:
http://iphone.zcentric.com/2008/08/05/custom-uitableviewcell/
http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/
http://www.e-string.com/content/custom-uitableviewcells-interface-builder
Including:
http://stackoverflow.com
http://www.google.com
Make your own UITableViewCell subclass.
Use a UITableView with a custom cell with whatever you want in it, load extra images and labels. To get the custom cell, create an IBOutlet to the cell and use this method.
[[NSBundle mainBundle] loadNibNamed:#"customCellView" owner:self options:nil];
To make a cell, make a new Nib/Xib file which is blank, make files owner the class with the cells, drag a UITableviewcell object out and put whatever objects you want on top of that view, set background as clear color and when you load the nib, enter all info into those images and labels. GL
Here is a block of code I use alot in my apps. Its not the fastest way to implement, but it gives you complete control over how the cell looks and whats in it. Using the code below you can see how my app looks. You have 2 buttons on one cell. They both do something different when pushed, and if the actual CELL is selected I do something else. I removed some of the code because im sure you dont care about what Im doing when the cell is selected, only how to get the buttons on, and know which one is pushed.
-(void)scheduleServiceButtonPressed:(id)sender {
//when the user click the button "service appt" on a table cell, we get the cellRow and use that to identify which car to show
ServiceApptViewController * vc = (ServiceApptViewController *)[[ServiceApptViewController alloc] init];
vc.cameFromGarageSelectionInt = [sender tag]; // here is the cell row that was selected.
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [app.savedUserVehicleArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell = [self getCellContentView:CellIdentifier:indexPath.row];
[cell setBackgroundColor:[UIColor redColor]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSLog(#"out cells for index");
return cell;
}
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier:(int)cellIndex {
NSLog(#"in content");
vehicleForRow = [app.savedUserVehicleArray objectAtIndex:cellIndex];
//CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect CellFrame = CGRectMake(0, 0, 320, 70);
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
// put a UIView underneath for coloring
UIView * view = [[UIView alloc] initWithFrame:CellFrame];
if ( cellIndex%2 == 0 ){
view.backgroundColor = [UIColor whiteColor];
}else{
//view.backgroundColor = [UIColor colorWithRed:0.98 green:0.92 blue:0.52 alpha:1.0];
view.backgroundColor = [UIColor colorWithRed:.238 green:.238 blue:0.238 alpha:.10];
}
[cell.contentView addSubview:view];
[view release];
if (vehicleForRow.isDefault && [vehicleForRow.isDefault compare:#"YES"]==0) {
//add green check mark if vehicle is default
UIImageView * bgimage3 = [[UIImageView alloc] initWithFrame:CGRectMake(245, 15, 40, 32)];
bgimage3.image = [UIImage imageNamed:#"greenCheckMark.png"];
[cell.contentView addSubview:bgimage3];
[bgimage3 release];
//default vehicle label
UILabel *lblTemp;
NSString * z = [NSString stringWithFormat:#"Default"];
NSString * s1 = z;
CGRect Label1Frame = CGRectMake(240, 43, 250, 25); // name
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.adjustsFontSizeToFitWidth = TRUE;
lblTemp.text = s1;
lblTemp.font = [UIFont boldSystemFontOfSize:12];
lblTemp.textColor = [UIColor blueColor];
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:lblTemp];
}
else {
UIImageView * bgimage3 = [[UIImageView alloc] initWithFrame:CGRectMake(250, 15, 30, 24)];
bgimage3.image = [UIImage imageNamed:#"grayCheckMark.png"];
[cell.contentView addSubview:bgimage3];
[bgimage3 release];
UILabel *lblTemp;
NSString * z = [NSString stringWithFormat:#"Set As Default"];
NSString * s1 = z;
CGRect Label1Frame = CGRectMake(233, 38, 250, 25); // name
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.adjustsFontSizeToFitWidth = TRUE;
lblTemp.text = s1;
lblTemp.font = [UIFont boldSystemFontOfSize:8];
lblTemp.textColor = [UIColor grayColor];
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:lblTemp];
}
// add service button to each cell
UIImage *image;
schedServiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
image = [UIImage imageNamed:#"tableServiceButton.png"];
[schedServiceButton setBackgroundImage:image forState:UIControlStateNormal];
schedServiceButton.frame = CGRectMake(5, 30, 97, 35);
[schedServiceButton setTag:cellIndex];//this is how we know which cell button was pressed
[schedServiceButton addTarget:self action:#selector(scheduleServiceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
schedServiceButton.titleLabel.font = [UIFont systemFontOfSize:12];
[schedServiceButton.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap];
[schedServiceButton setTitle:#"Schedule\nService Appt." forState:UIControlStateNormal];
schedServiceButton.titleLabel.textAlignment = UITextAlignmentCenter;
[cell.contentView addSubview:schedServiceButton];
//yes add owners manual button
viewOMButton = [UIButton buttonWithType:UIButtonTypeCustom];
image = [UIImage imageNamed:#"tableOMButton.png"];
[viewOMButton setBackgroundImage:image forState:UIControlStateNormal];
viewOMButton.frame = CGRectMake(105, 30, 97, 35);
[viewOMButton setTag:cellIndex];
[viewOMButton addTarget:self action:#selector(viewOwnersManualButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
viewOMButton.titleLabel.font = [UIFont systemFontOfSize:12];
[viewOMButton.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap];
[viewOMButton setTitle:#"View\nOwner's Manual" forState:UIControlStateNormal];
viewOMButton.titleLabel.textAlignment = UITextAlignmentCenter;
[cell.contentView addSubview:viewOMButton];
//car description label
UILabel *lblTemp;
NSString * z = [NSString stringWithFormat:#"%# %# %#",vehicleForRow.userVehicleYear,vehicleForRow.userVehicleMake,vehicleForRow.userVehicleModel];
NSString * s1 = z;
CGRect Label1Frame = CGRectMake(10, 5, 250, 25); // name
//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.adjustsFontSizeToFitWidth = TRUE;
lblTemp.text = s1;
lblTemp.font = [UIFont boldSystemFontOfSize:16];
lblTemp.textColor = [UIColor blueColor];
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:lblTemp];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}