Label in UITableCellView.contentView showing two times when using autoresizing - ios

I'm using Grouped UITableView and add UITableViewCells two UILabel's and adding them to the contentView of the cell. I am also implementing edit mode in my app on this UITableView.
I use the
lblDetail.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; in order to decrease width of the text so the delete red button showing in edit mode will not cover my text.
Now what I see is when I scroll my table not in edit mode the labels show two times as if it is in in edit mode and out of edit mode at the same time.
This is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
CGRect DetailLabelFrame = CGRectMake(52, 25, 200, 35);
CGRect TitleLabelFrame = CGRectMake(52, 5, 200, 25);
lblDetail = [[UILabel alloc]init];
lblTitle = [[UILabel alloc]init];
lblDetail.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
lblTitle.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
lblDetail.frame = DetailLabelFrame;
lblDetail.lineBreakMode = UILineBreakModeWordWrap;
lblDetail.backgroundColor = [UIColor clearColor];
lblDetail.numberOfLines = 2;
lblTitle.frame = TitleLabelFrame;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.frame = TitleLabelFrame;
[lblDetail setFont:[UIFont fontWithName:#"Arial" size:14]];
[lblTitle setFont:[UIFont fontWithName:#"Arial-BoldMT" size:15]];
lblDetail.textAlignment = UITextAlignmentRight;
lblTitle.textAlignment = UITextAlignmentRight;
lblTitle.text = #"054-9700583";
lblDetail.text = #"שאלה:במה אפשר לעזור לך?";
[cell.contentView addSubview:lblDetail];
[cell.contentView addSubview:lblTitle];
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
cell.imageView.image = [UIImage imageNamed:#"girl.png"];
[lblDetail release];
[lblTitle release];
return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
CGRect DetailLabelFrame = CGRectMake(52, 25, 200, 35);
CGRect TitleLabelFrame = CGRectMake(52, 5, 200, 25);
lblDetail = [[UILabel alloc]init];
lblTitle = [[UILabel alloc]init];
lblDetail.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
lblTitle.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
lblDetail.frame = DetailLabelFrame;
lblDetail.lineBreakMode = UILineBreakModeWordWrap;
lblDetail.backgroundColor = [UIColor clearColor];
lblDetail.numberOfLines = 2;
lblTitle.frame = TitleLabelFrame;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.frame = TitleLabelFrame;
[lblDetail setFont:[UIFont fontWithName:#"Arial" size:14]];
[lblTitle setFont:[UIFont fontWithName:#"Arial-BoldMT" size:15]];
lblDetail.textAlignment = UITextAlignmentRight;
lblTitle.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:lblDetail];
[cell.contentView addSubview:lblTitle];
lblDetail.tag = 10;
lblTitle.tag = 11;
}
lblDetail = (UILabel*)[cell.contentView viewWithTag:10];
lblTitle = (UILabel*)[cell.contentView viewWithTag:11];
lblTitle.text = #"054-9700583";
lblDetail.text = #"שאלה:במה אפשר לעזור לך?";
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 5.0;
cell.imageView.image = [UIImage imageNamed:#"girl.png"];
[lblDetail release];
[lblTitle release];
return cell;
}
Your cell contents was overlapping may be it will help you.Still now if you face same problem the knock me without any hesitation.

If this is only happening when you're scrolling it may be because when a cell is being reused it's creating a new set of labels in addition to what's already in the reused cell.
What you can try to do is move these two lines into your if (cell == nil ) block:
lblDetail = [[UILabel alloc]init];
lblTitle = [[UILabel alloc]init];
That way when a cell is being reused it'll continue to use the existing lblDetail and lblTitle instead of creating a new one.

Related

UIlabel with Custom cell not appear

I'm confused !, it is not my first time to create Custom Cell but this time UILabel doesn't appear I don't know where is the problem ?
This the custom cell Cell.m
mainView = [[UIView alloc] initWithFrame:CGRectZero];
mainView.backgroundColor = [UIColor colorWithRed:249.0/255.0 green:249.0/255.0 blue:249.0/255.0 alpha:1.0];
profile = [[UIImageView alloc] initWithFrame:CGRectZero];
profile.layer.masksToBounds = YES;
profile.layer.cornerRadius = 22.5;
profile.layer.borderColor = [UIColor whiteColor].CGColor;
profile.layer.borderWidth = 1.0f;
profile.clipsToBounds = YES;
tweet_is = [[UILabel alloc] initWithFrame:CGRectZero];
tweet_is.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:20];
tweet_is.backgroundColor = [UIColor clearColor];
tweet_is.textColor = [UIColor blackColor];
[self.mainView addSubview:tweet_is];
[self.mainView addSubview:profile];
[self.contentView addSubview:self.mainView];
-(void)layoutSubviews {
[super layoutSubviews];
self.mainView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 10.0f);
self.profile.frame = CGRectMake(15, 15, 45, 45);
self.tweet_is.frame = CGRectMake(30, 30, 300.0f, 300.0f);
}
This is the tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
Cell *cell = (Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.tweet_is.text = #"Not approve";
return cell;
}
Change this line:
self.tweet_is.frame = CGRectMake(30, 30, 300.0f, 300.0f);
set it self.tweet_is.frame = CGRectMake(30, 30, 300.0f, 30.0f);
If your cell height is less than 300 (I think it is) then you can not see the text. Let me know if that helps.. :)

UITableView puts cell content on wrong rows after scroll

Although there are couple of posts in the site regarding UITableview cell repetition when scrolled, I could nt find the solution, perhaps because am pretty new to obj-c.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = [NSString stringWithFormat:#"cell%d",indexPath.row];
UILabel *firstlabel = nil;
UILabel *secondlabel = nil;
UILabel *thirdlabel = nil;
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if(!cell) {
cell = [[myTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
if (noOfRows < [_eachrow count]-1) {
_eachcolumn = [_eachrow[noOfRows] componentsSeparatedByString:#"|"];
firstlabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 142.0,tableView.rowHeight)];
firstlabel.font = [UIFont fontWithName:#"Verdana" size:12];
firstlabel.text = _eachcolumn[0];
firstlabel.textAlignment = ALIGN_LEFT;
firstlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:firstlabel];
secondlabel = [[UILabel alloc] initWithFrame:CGRectMake(142.0, 0, 50.0,tableView.rowHeight)];
secondlabel.font = [UIFont fontWithName:#"Verdana" size:12];
secondlabel.text = _eachcolumn[1];
secondlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:secondlabel];
thirdlabel = [[UILabel alloc] initWithFrame:CGRectMake(192.0, 0, 60.0,tableView.rowHeight)];
thirdlabel.font = [UIFont fontWithName:#"Verdana" size:12];
thirdlabel.text = _eachcolumn[2];
thirdlabel.textAlignment = ALIGN_CENTER;
thirdlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:thirdlabel];
noOfRows++;
return cell;
}
else return cell;
}
So, my problem is when I scroll up or down, the cells keep placed at wrong rows without a pattern. Please help me resolve this issue. Thanks in advance
The problem occurs because the cells are reusable and you need to override the values each time in cellForRow .
Try this instead:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = [NSString stringWithFormat:#"cell%d",indexPath.row];
myTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell) {
cell = [[myTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UILabel *firstlabel = nil;
UILabel *secondlabel = nil;
UILabel *thirdlabel = nil;
_eachcolumn = [_eachrow[noOfRows] componentsSeparatedByString:#"|"];
firstlabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 142.0,tableView.rowHeight)];
firstlabel.font = [UIFont fontWithName:#"Verdana" size:12];
firstlabel.text = _eachcolumn[0];
firstlabel.textAlignment = ALIGN_LEFT;
firstlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
firstlabel.tag = 10;
[cell.contentView addSubview:firstlabel];
secondlabel = [[UILabel alloc] initWithFrame:CGRectMake(142.0, 0, 50.0,tableView.rowHeight)];
secondlabel.font = [UIFont fontWithName:#"Verdana" size:12];
secondlabel.text = _eachcolumn[1];
secondlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
secondlabel.tag = 20;
[cell.contentView addSubview:secondlabel];
thirdlabel = [[UILabel alloc] initWithFrame:CGRectMake(192.0, 0, 60.0,tableView.rowHeight)];
thirdlabel.font = [UIFont fontWithName:#"Verdana" size:12];
thirdlabel.text = _eachcolumn[2];
thirdlabel.textAlignment = ALIGN_CENTER;
thirdlabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
thirdlabel.tag = 30;
[cell.contentView addSubview:thirdlabel];
return cell;
}
else
{
((UILabel *)[cell.contentView viewWithTag:10]).text = _eachcolumn[1];
((UILabel *)[cell.contentView viewWithTag:20]).text = _eachcolumn[2];
((UILabel *)[cell.contentView viewWithTag:30]).text = _eachcolumn[3];
return cell;
}
}

How to optimize cellForRowAtIndexPath: code

After adding a font and a shadow to some UILabels I noticed that the table view animation lags when the view is popped off the stack (a side swipe like FB/Path uses). The side swipe was smooth until I added the UILabel shadows.
I think I might be adding it it the wrong place so that the label properties are being added incorrectly maybe. Please take a look at the following cellForRowAtIndexPath: method below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.image = [UIImage imageNamed:#"rest.jpg"];
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}
thanks for any help.
You're always adding new subviews. So whenever you scroll the table view your cells are getting more and more content added into them.
Create all your subviews when the cell is created and then just update the subviews settings. Something like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.tag = 123123;
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.tag = 234234];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.tag = 345345];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
}
UIImageView *imageView = (UIImageView *)[cell viewWithTag:123123];
imageView.image = [UIImage imageNamed:#"rest.jpg"];
UILabel *titleLabel = (UILabel *)[cell viewWithTag:234234];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
UILabel *detailLabel = (UILabel *)[cell viewWithTag:345345];
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
return cell;
}
Since text attributes never change, move the code that sets them inside the if statement. Keep only the code that sets the image and the text of your labels outside the if statement. Cells are reused, so the attributes such as font etc. will remain with the cell even after it gets "recycled". In the else branch add code that finds existing labels in the cell. Otherwise, you keep adding the same label to the cell multiple times.
You add subviews, even after dequeueing instead of initializing a new cell. Make sure all the code that creates and adds subviews are only done on initializing a cell. If you need to refer to views in the cell for configuring, subclass UITableViewCell.
Also, shadow rendering could be slowing it down too, add a shadowpath to make the rendering more efficient:
add to your tableView:cellForRowAtIndexPath: method:
...
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:detailLabel.layer.bounds].CGPath;
detailLabel.layer.shadowPath = shadowPath;
...
This article from Twitter Engineering gives you a great overview: http://engineering.twitter.com/2012/02/simple-strategies-for-smooth-animation.html
Basically, you want to avoid using subviews but instead draw your content directly with Quartz. It's the single best thing you can do to improve performance. Also: Avoid transparency! In Instruments, you can also use the Core Animation instrument and activate "Color Blended Layers" to see where transparent views are being composed:
Replace your code with this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 2, self.view.bounds.size.width, 200)];
imageView.image = [UIImage imageNamed:#"rest.jpg"];
imageView.tag =1;
[cell.contentView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, 320, 30)];
titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.tag = 2;
titleLabel.textColor = [UIColor whiteColor];
[titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:24]];
titleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
titleLabel.layer.shadowOpacity = 0.7;
[cell.contentView addSubview:titleLabel];
UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, cell.bounds.size.width, 30)];
detailLabel.tag = 3;
detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.textColor = [UIColor whiteColor];
[detailLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:18]];
detailLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
detailLabel.layer.shadowOpacity = 0.7;
}
UIImageView *tempImgView = (UIImageView *)[cell viewWithTag:1];
tempImgView.image = [UIImage imageNamed:#""];// Here you can set any image by reusing imageview without allocating again and again
UILabel *tempLabel;
tempLabel = (UILabel *)[cell viewWithTag:2];
tempLabel.text = #"";// Here you can access your title label and can set its properties without allocating again
tempLabel = (UILabel *)[cell viewWithTag:3];
tempLabel.text = #"";// Here you can access your detailLabel label and can set its properties without allocating again
[cell.contentView addSubview:detailLabel];
cell.contentView.backgroundColor = [UIColor clearColor];
return cell;
}
You need create custom table view cell with a class. This code you added many label then shadow show no correctly.
code like this.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellReuseIdentifier = #"cellReuseIdentifier";
UITableCustomViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentifier];
if (cell == nil)
{
cell = [[UITableCustomViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellReuseIdentifier];
}
cell.imageView.image = [UIImage imageNamed:#"rest.jpg"];
cell.titleLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.detailLabel.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"description"];
return cell;
}

UILabel -- WordWrap -- Frame Issue

A UILabel in my App is behaving unusually and I'm wondering if anyone has any ideas or suggestions on how to resolve it.
1.First I set the frames of the labels which will both appear inside the cell of a tableView.
CGRect CellFrame = CGRectMake(0, 0, 300, 75);
CGRect Label1Frame = CGRectMake(10, 5, 290, 20);
CGRect Label2Frame = CGRectMake(0, 20, 290, 50);
UILabel *lblTemp;
UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier];
Then I set the properties of labels
1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
lblTemp.font = [UIFont fontWithName:#"Helvetica" size:12.0];
lblTemp.textColor = [UIColor blackColor];
lblTemp.lineBreakMode = UILineBreakModeWordWrap;
lblTemp.numberOfLines = 0;
[cell.contentView addSubview:lblTemp];
//Initialize Label with tag 2.
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
lblTemp.tag = 2;
lblTemp.font = [UIFont fontWithName:#"Helvetica" size:10];
lblTemp.textColor = [UIColor lightGrayColor];
lblTemp.lineBreakMode = UILineBreakModeWordWrap;
lblTemp.numberOfLines = 0;
[cell.contentView addSubview:lblTemp];
return cell;
3.Then I setup the contents of the cell
//set up the cell
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];
CaseFeed *aCaseFeed = [[CaseFeed alloc] init];
aCaseFeed = [CaseFeedbook objectAtIndex:indexPath.row];
lblTemp1.text = [NSString stringWithFormat:#"%#", aCaseFeed.title];
lblTemp2.text = [NSString stringWithFormat:#"%#", aCaseFeed.description];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
PROBLEM: The contents of lblTemp2 (the second label in the cell) will span 2 or more lines. During runtime, when the text of lblTemp2 is populated, the first two lines appear perfectly, however the third line starts outside the left side of the frame.
Any ideas on how to resolve this? Why is this happening?
Use the following code to setup your cell of a tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else
{
for (id v in cell.contentView.subviews)
{
UIView *realV = (UIView *) v;
[realV removeFromSuperview];
}
}
CGRect CellFrame = CGRectMake(0, 0, 300, 75);
CGRect Label1Frame = CGRectMake(10, 5, 290, 20);
CGRect Label2Frame = CGRectMake(0, 20, 290, 50);
UILabel *label1 = [[UILabel alloc] init];
label1.frame = Label1Frame;
label1.tag = 1;
label1.font = [UIFont fontWithName:#"Helvetica" size:12.0];
label1.textColor = [UIColor blackColor];
label1.lineBreakMode = UILineBreakModeWordWrap;
label1.numberOfLines = 0;
[cell.contentView addSubview:label1];
UILabel *label2 = [[UILabel alloc] init];
label2.frame = Label2Frame;
label2.tag = 2;
label2.font = [UIFont fontWithName:#"Helvetica" size:10.0];
label2.textColor = [UIColor lightGrayColor];
label2.lineBreakMode = UILineBreakModeWordWrap;
label2.numberOfLines = 0;
[cell.contentView addSubview:label2];
CaseFeed *aCaseFeed = [[CaseFeed alloc] init];
aCaseFeed = [CaseFeedbook objectAtIndex:indexPath.row];
label0.text = [NSString stringWithFormat:#"%#", aCaseFeed.title];
label1.text = [NSString stringWithFormat:#"%#", aCaseFeed.description];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Hope this helps

Why old value appeared in tableViewCell

I added a UILabel when TableViewCell making. Code like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Special *special = [speciales objectAtIndex:indexPath.row];
......
UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
description.text = special.specialDescription;
description.font = [UIFont fontWithName:#"Heiti SC" size:12];
description.textColor = [UIColor darkGrayColor];
description.lineBreakMode = UILineBreakModeWordWrap;
description.numberOfLines = 3;
[cell addSubview:description];
return cell;
}
It works well, but when I scrolled it from the bottom to top, and when I selected a row, the old value appeared at the same time. Who can help me fix this?
Thank you!
update : All of my codes is this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Special *special = [speciales objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 60, 60)];
img.image = special.specialIconImage;
[self addShadowToImage:img];
[cell addSubview:img];
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 220, 16)];
name.text = special.specialName;
name.font = [UIFont fontWithName:#"Heiti SC" size:16];
[cell addSubview:name];
UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
description.text = special.specialDescription;
description.font = [UIFont fontWithName:#"Heiti SC" size:12];
description.textColor = [UIColor darkGrayColor];
description.lineBreakMode = UILineBreakModeWordWrap;
description.numberOfLines = 3;
[cell addSubview:description];
return cell;
}
The problem is that you're adding the Image and Label subviews every time the cell is being called. Instead, you only want to add those subviews when you're creating the cell. Every time the cell gets called you just want to set the values of subviews. You'll want something like this (done off memory):
Special *special = [speciales objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 60, 60)];
[imageView setTag:100];
[cell addSubview:imageView];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 220, 16)];
name.font = [UIFont fontWithName:#"Heiti SC" size:16];
[nameLabel setTag:101];
[cell addSubview:nameLabel];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
[descriptionLabel setTag:102];
descriptionLabel.font = [UIFont fontWithName:#"Heiti SC" size:12];
descriptionLabel.textColor = [UIColor darkGrayColor];
descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
descriptionLabel.numberOfLines = 3;
[cell addSubview:descriptionLabel];
}
UIImageView *img = (UIImageView *)[cell viewWithTag:100];
img.image = special.specialIconImage;
[self addShadowToImage:img];
UILabel *name = (UILabel *)[cell viewWithTag:101];
name.text = special.specialName;
UILabel *description = (UILabel *)[cell viewWithTag:102];
description.text = special.specialDescription;
return cell;
[tableView reloadData];
try this.
While I think you should be dequeueing cells to reuse them, it doesn't seem to be the root of your problem - unless you are reusing cells and didn't show us. There could be code there that points to the issue.
I think the issue could be related to redrawing. Try putting this at the end of your tableView:cellForRowAtIndex: method
[cell setNeedsDisplay];
This will force the UI to redraw the cell.
What does ..... mean in the code?
Are you using dequeueReusableCellWithIdentifier in there?
If That part of the code conaitns the code something like below, then It should work just fine-
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
Here are some things to keep in mind:
• The new views must be added when we instantiate a new cell, but not when we
reuse a cell (because a reused cell already has them).
• We must never send addSubview: to the cell itself — only to its contentView (or
some subview thereof).
• We should assign the new views an appropriate autoresizingMask, because the
cell’s content view might be resized.
• Each new view should be assigned a tag so that it can be referred to elsewhere.
Here is code:
//thanks to Thomas Hajcak's code
Special *special = [speciales objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 60, 60)];
[imageView setTag:100];
//autoresizingMask as follow
imageView.autoresizingMask = (UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleLeftMargin);
[cell.contentView addSubview:imageView];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 5, 220, 16)];
name.font = [UIFont fontWithName:#"Heiti SC" size:16];
[nameLabel setTag:101];
[cell.contentView addSubview:nameLabel];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, 220, 50)];
[descriptionLabel setTag:102];
descriptionLabel.font = [UIFont fontWithName:#"Heiti SC" size:12];
descriptionLabel.textColor = [UIColor darkGrayColor];
descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
descriptionLabel.numberOfLines = 3;
[cell.contentView addSubview:descriptionLabel];
}
UIImageView *img = (UIImageView *)[cell.contentView viewWithTag:100];
img.image = special.specialIconImage;
[self addShadowToImage:img];
UILabel *name = (UILabel *)[cell.contentView viewWithTag:101];
name.text = special.specialName;
UILabel *description = (UILabel *)[cell.contentView viewWithTag:102];
description.text = special.specialDescription;
return cell;

Resources