10-23-2011, 12:48 AM
#1 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 3
tableview cell text are overlapping while scrolling…why?
I have UItableview where every cell is showing some data in 3 UILabel..all of these three label are in a UIView and UIView is the contentview of the cell.Now when i run the project at first table cell's are displaying fine but when i start scroll the tableview then cell text starts overlapping each other. please help me what is the solution of the problem.i have got a lot of similar question but these are no appropriate for my code so anybody please help me...this my code...
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cellimage =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 275, 30)];
[cell.contentView addSubview:cellimage];
CGRect attributeNameRect = CGRectMake(0, 0, 138, 29);
attributeName = [[UILabel alloc] initWithFrame:attributeNameRect];
attributeName.textAlignment = UITextAlignmentCenter;
attributeName.textColor = [UIColor blackColor];
attributeName.font = [UIFont systemFontOfSize:12];
attributeName.backgroundColor = [UIColor clearColor];
attributeName.layer.borderColor = [UIColor grayColor].CGColor;
attributeName.layer.borderWidth = 1.0;
attributeName.tag = 1;
// [attributeName release];
CGRect nameValueRect = CGRectMake(138, 0, 57, 29);
nameValue = [[UILabel alloc] initWithFrame:nameValueRect];
nameValue.textAlignment = UITextAlignmentCenter;
nameValue.textColor = [UIColor blackColor];
nameValue.font = [UIFont systemFontOfSize:12];
nameValue.backgroundColor = [UIColor clearColor];
nameValue.layer.borderColor = [UIColor grayColor].CGColor;
nameValue.layer.borderWidth = 1.0;
nameValue.tag = 2;
// [nameValue release];
CGRect PercentageRect = CGRectMake(195, 0, 80, 29);
visitorPercentage = [[UILabel alloc] initWithFrame:PercentageRect];
visitorPercentage.textAlignment = UITextAlignmentCenter;
visitorPercentage.textColor = [UIColor blackColor];
visitorPercentage.font = [UIFont systemFontOfSize:12];
visitorPercentage.backgroundColor = [UIColor clearColor];
visitorPercentage.layer.borderColor = [UIColor grayColor].CGColor;
visitorPercentage.layer.borderWidth = 1.0;
visitorPercentage.tag = 3;
// [visitorPercentage release];
}
switch (indexPath.row) {
case 0:
{
attributeName.text = @"visitor:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:0]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 1:
{
attributeName.text = @"uniqueVisitor:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:1]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 2:
{
attributeName.text = @"order:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:2]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 3:
{
attributeName.text = @"revenue:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:3]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 4:
{
attributeName.text = @"conversionRate:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:4]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 5:
{
attributeName.text = @"newProduct:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:5]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 6:
{
attributeName.text = @"outOfStockProduct:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:6]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 7:
{
attributeName.text = @"productInCart:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:7]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 8:
{
attributeName.text = @"visitorInc:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:8]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 9:
{
attributeName.text = @"uniqueVisitorInc:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:9]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 10:
{
attributeName.text = @"orderInc:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:10]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 11:
{
attributeName.text = @"revenueInc:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:11]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 12:
{
attributeName.text = @"ConvertionRateInc:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:11]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 13:
{
attributeName.text = @"newProductInc:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:11]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 14:
{
attributeName.text = @"outOfStockProductInc:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:11]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
case 15:
{
attributeName.text = @"productInCartInc:";
[cellimage addSubview:attributeName];
nameValue.text = [NSString stringWithFormat:@"%@",[[records objectAtIndex:0] objectAtIndex:11]];
[cellimage addSubview:nameValue];
visitorPercentage.text = @"25%";
[cellimage addSubview:visitorPercentage];
break;
}
default:
break;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 30;
}
Last edited by Emon; 10-23-2011 at 01:37 AM .
10-23-2011, 01:08 AM
#2 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
Because, UITableViews reuse cells. Every time a row comes into view, it calls cellForRowAtIndexPath. Every time this happens, you are adding a label to "cellimage".
Add all your cellimage subviews and tag them inside the cell == nil. Then get references to them outside your cell == nil by calling viewWithTag on cellimage.
Also, where did you define cellimage and all your other subviews?
And one more thing, please use code tags from now, [code] [1/CODE] without the 1.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
10-23-2011, 01:26 AM
#3 (permalink )
Registered Member
Join Date: Oct 2011
Posts: 3
[code]
Quote:
Originally Posted by
Domele
Because, UITableViews reuse cells. Every time a row comes into view, it calls cellForRowAtIndexPath. Every time this happens, you are adding a label to "cellimage".
Add all your cellimage subviews and tag them inside the cell == nil. Then get references to them outside your cell == nil by calling viewWithTag on cellimage.
Also, where did you define cellimage and all your other subviews?
And one more thing, please use code tags from now,
Code:
[1/CODE] without the 1.
thanks brother...it works now
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 391
14 members and 377 guests
13dario13 , 7twenty7 , buggen , EvilElf , glenn_sayers , j.b.rajesh@gmail.com , LunarMoon , morterbaher , n00b , QuantumDoja , sacha1996 , Sami Gh , VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher