Custom uitableview cell issue - iphone

I have a custom UITableViewCell as such:
#implementation CellWithThreeSubtitles
#synthesize title, subTitle1, subTitle2, subTitle3;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.textLabel.backgroundColor = self.backgroundColor;
self.textLabel.opaque = NO;
self.textLabel.textColor = [UIColor blackColor];
self.textLabel.highlightedTextColor = [UIColor whiteColor];
self.textLabel.font = [UIFont boldSystemFontOfSize:22.0];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGRect frame = CGRectMake(35.0, 0, contentRect.size.width, 50.0);
self.textLabel.frame = frame;
UILabel *subLabel1 = [[UILabel alloc] init];
frame = CGRectMake(35.0, 34.0, contentRect.size.width, 25.0);
subLabel1.font = [UIFont systemFontOfSize:18.0];
subLabel1.backgroundColor = [UIColor clearColor];
subLabel1.text = subTitle1;
subLabel1.opaque = NO;
subLabel1.frame = frame;
[self.contentView addSubview:subLabel1];
UILabel *subLabel2 = [[UILabel alloc] init];
frame = CGRectMake(35.0, 54.0, contentRect.size.width, 25.0);
subLabel2.font = [UIFont boldSystemFontOfSize:18.0];
subLabel2.backgroundColor = [UIColor clearColor];
subLabel2.text = subTitle2;
subLabel2.opaque = NO;
subLabel2.frame = frame;
[self.contentView addSubview:subLabel2];
UILabel *subLabel3 = [[UILabel alloc] init];
frame = CGRectMake(contentRect.size.width-100.0, 54.0, contentRect.size.width, 25.0);
subLabel3.font = [UIFont systemFontOfSize:18.0];
subLabel3.backgroundColor = [UIColor clearColor];
subLabel3.text = subTitle3;
subLabel3.opaque = NO;
subLabel3.frame = frame;
[self.contentView addSubview:subLabel3];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[super dealloc];
[subTitle4 release];
[subTitle3 release];
[subTitle2 release];
[subTitle1 release];
[title release];
}
When I implement it in my UITableView like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int section = [indexPath indexAtPosition:0];
static NSString *kCustomCellID = #"AppointmentCellID";
CellWithThreeSubtitles *cell = (CellWithThreeSubtitles *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil) {
cell = (CellWithThreeSubtitles *)[[[CellWithThreeSubtitles alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
}
switch (section) {
case 0:
if ([wineArray count]==0) {
cell.textLabel.text = #"No wine favorites selected yet";
}else{
cell.subTitle1 = [[wineArray objectAtIndex:indexPath.row] objectForKey:#"Varietal"];
cell.subTitle2 = [NSString stringWithFormat:#"Bin No. %#", [[wineArray objectAtIndex:indexPath.row] objectForKey:#"Bin"]];
cell.subTitle3 = [NSString stringWithFormat:#"$%#", [[wineArray objectAtIndex:indexPath.row] objectForKey:#"Price"]];
cell.textLabel.text = [NSString stringWithFormat:#"%# (%#)", [[wineArray objectAtIndex:indexPath.row] objectForKey:#"Name"], [[wineArray objectAtIndex:indexPath.row] objectForKey:#"Availability"]];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
break;
case 1:
if ([dinnerArray count]==0)
cell.textLabel.text = #"No dinner favorites selected yet";
else
cell.textLabel.text = [NSString stringWithFormat:#"%#", [[dinnerArray objectAtIndex:indexPath.row] objectForKey:#"Name"]];
break;
}
return cell;
}
the contents of the cell duplicates on top of itself every time the orientation of the device changes. Is it redrawing the cell every time the device rotates? And if so, How can I keep it from doing so?

You should be creating the subviews and adding them to your cell's subviews array in your -initWithStyle:reuseIdentifier: method. You only need to create the subviews once. The -layoutSubviews method is invoked repeatedly by the framework whenever the device orientation changes, to allow you to resize and/or move the subviews (plus make any other minor adjustments) to compensate for differences between the orientations.

I suspect layoutSubViews is being called on rotation, and re-drawing new labels on top of your old ones. If you remove any labels from the table cell at the beginning of layoutSubViews that would probably fix that issue, or you could create them in the init and then just position them in layoutSubViews.

Related

UITableViewCell/UITextField draw issue when Datasource is larger than row number

So, this is driving me nuts...
Everything works great, unless my datasource: an NSMutableArrray: _names contains more items than there are rows visible in the UITableView: namesTable.
then all hell breaks loose... cells are overlaying one another, there are empty cells, repeating cells.
here is my setup code.
#interface DetailViewController ()
{
NSMutableArray *_names;
}
My viewDidLoad method:
- (void) viewDidLoad
{
[super viewDidLoad];
self.navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleBlackOpaque];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(handleBack:)];
self.navigationItem.leftBarButtonItem = backButton;
if (!_names)
_names = [[NSMutableArray alloc] init];
}
and cellForRowAtIndexPath:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"MyCustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
UITextField *nameTextField;
if (!cell || refreshCells)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
self.view.bounds.origin.y+10,
self.view.bounds.size.width-19,
26.0)];*/
nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x+10,
self.view.bounds.origin.y+4,
self.view.bounds.size.width-19,
34.0)];
}
nameTextField.adjustsFontSizeToFitWidth = YES;
nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
nameTextField.backgroundColor = [UIColor clearColor];
nameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
nameTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
nameTextField.textAlignment = UITextAlignmentLeft;
nameTextField.font = [UIFont fontWithName:#"Noteworthy-Bold" size:22.0];
nameTextField.keyboardType = UIKeyboardTypeDefault;
nameTextField.returnKeyType = UIReturnKeyDone;
nameTextField.clearButtonMode = UITextFieldViewModeNever;
nameTextField.delegate = self;
nameTextField.userInteractionEnabled = NO;
NSString *object = _names[indexPath.row];
nameTextField.text = [object description];
[cell.contentView addSubview:nameTextField];
cell.selectionStyle = UITableViewCellEditingStyleNone;
return cell;
}
can anybody, please tell show me what I am doing wrong?
You can use XIB to design your cell, instead of writing this much code. Also what is that refreshCells?? Also where are you adding data to your Array??
Better try to design the cell in XIB and IBOutlet it. Then load it in cellForRow...
In your code every time you are adding one textfield to your cell
[cell.contentView addSubview:nameTextField];
which is not necessary at all..
This could solve your problem.
I ended up subclassing UITableViewCell, and moving most of this code into:
#implementation DetailViewCustomCell
#synthesize nameTextField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
nameTextField = [[UITextField alloc] init];
nameTextField.adjustsFontSizeToFitWidth = YES;
nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
nameTextField.backgroundColor = [UIColor clearColor];
nameTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
nameTextField.textAlignment = UITextAlignmentLeft;
nameTextField.font = [UIFont fontWithName:#"Noteworthy-Bold" size:22.0];
nameTextField.returnKeyType = UIReturnKeyDone;
nameTextField.userInteractionEnabled = NO;
[self.contentView addSubview:nameTextField];
self.selectionStyle = UITableViewCellEditingStyleNone;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x+10;
CGFloat boundsY = contentRect.origin.x+4;
CGFloat boundsW = contentRect.size.width-19;
CGRect frame;
frame= CGRectMake(boundsX ,boundsY, boundsW, 34.0);
nameTextField.frame = frame;
}
and in my view controller:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
DetailViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[DetailViewCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSString *object = _names[indexPath.row];
cell.nameTextField.text = [object description];
cell.nameTextField.delegate = self;
return cell;
}
this worked perfectly!

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.

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.

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.