Reputation: 609
This is my code every time when i scroll my labels text are getting overleaped.Text are overlapping and i think this is because reuse of cell. I am using storyboard and this is not a custom cell. Need some help,every single help would be appreciated.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *TableViewCEll =@"TableViewCell";
UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:TableViewCEll forIndexPath:indexPath];
if (Cell == nil) {
Cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCEll];
}
UIImageView *imageView=[[UIImageView alloc]init];
imageView.frame =CGRectMake(5, 5, 100,110);
[Cell.contentView addSubview:imageView];
NSArray * array =[[self.ListOfCarArray objectAtIndex:indexPath.row]objectForKey:@"CarImage"];
if (array.count >0) {
NSString * str = [NSString stringWithFormat:@"%@uploads/Car/%@",[self.GetUserConfiDict objectForKey:@"server"],[[array objectAtIndex:0]objectForKey:@"name"]];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]init];
manager.responseSerializer = [AFImageResponseSerializer serializer];
[manager GET:str
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
imageView.image = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed with error %@.",[error localizedFailureReason]);
}];
}
UILabel * ModelLbl =[[UILabel alloc]initWithFrame:CGRectMake(120, 10, 120, 25)];
ModelLbl.textColor = [UIColor whiteColor];
NSDictionary *str = [[NSDictionary alloc]init];
str =[[self.ListOfCarArray objectAtIndex:indexPath.row]objectForKey:@"Car"];
ModelLbl.text = [str objectForKey:@"model"];
[Cell.contentView addSubview:ModelLbl];
int x = 120;
int y = 50;
for (int i = 1; i<=5; i++) {
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 84,15)];
label.textColor = [UIColor whiteColor];
if (i ==1) {
id null = [str objectForKey:@"year"];
if ([null isKindOfClass:[NSNull class]]) {
label.text =@"";
}
else{
label.text = [str objectForKey:@"year"];
[Cell.contentView addSubview:label];
}
}
else if (i == 2){
id null = [str objectForKey:@"color"];
if ([null isKindOfClass:[NSNull class]]) {
label.text =@"";
}
else{
label.text =[str objectForKey:@"color"];
[Cell.contentView addSubview:label];
}
}
else if (i == 3){
id null = [str objectForKey:@"price"];
if ([null isKindOfClass:[NSNull class]]) {
label.text =@"";
}
else{
label.text =[str objectForKey:@"price"];
[Cell.contentView addSubview:label];
x+=100;
y = 30;
}
}
else if (i ==4) {
id null = [str objectForKey:@"miles"];
if ([null isKindOfClass:[NSNull class]]) {
label.text =@"";
}
else{
label.text =[str objectForKey:@"miles"];
[Cell.contentView addSubview:label];
}
}
else if (i == 5){
id null = [str objectForKey:@"stock_number"];
if ([null isKindOfClass:[NSNull class]]) {
label.text =@"";
}
else{
label.text =[str objectForKey:@"stock_number"];
[Cell.contentView addSubview:label];
}
}
y+=20;
}
return Cell;
}
Upvotes: 1
Views: 45
Reputation: 22641
Yes, this is caused by the reuse of the table view cell. It is not only the labels but also the imageView. You are adding the imageView
with this method
[Cell.contentView addSubview:imageView];
every time, while it should only be done when the cell is created, i.e. inside the
if (Cell == nil) {
Cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCEll];
}
I'd advise to use a custom UITableViewCell subclass, but if you stick with the current approach, I'd do it this way:
static int IMAGE_VIEW_TAG = 1;
if (Cell == nil) {
Cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableViewCEll];
UIImageView *imageView = [[UIImageView alloc]init];
imageView.frame = CGRectMake(5, 5, 100,110);
imageView.tag = IMAGE_VIEW_TAG;
[Cell.contentView addSubview:imageView];
}
Further below, you can refer to the imageView by the tag:
((UIImageView *)[Cell.contentView viewWithTag:IMAGE_VIEW_TAG]).image = responseObject;
You'll have to do the same for the labels.
Upvotes: 0
Reputation: 609
i got my answer and this is what i was searching for..
for (UILabel *lbl in cell.contentView.subviews)
{
if ([lbl isKindOfClass:[UILabel class]])
{
[lbl removeFromSuperview];
}
}
Upvotes: 1