CellForRowAtIndexPath: UILabel overlaps after scrolling

cellForRowAtIndexPath:

cell.textLabel.text works fine.

UILabel overlaps after scrolling. Here is the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; if (cell==nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; // I have tried it by removing views and without. No difference. NSArray *viewsToRemove = [self.tableView subviews]; for (UITableView *table in viewsToRemove) { [table removeFromSuperview]; } } NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row]; NSString *entityName= [[managedObject entity]name]; cell.textLabel.text = [NSString stringWithFormat:@"%@ %i", entityName, [indexPath row]]; cell.textLabel.font=[UIFont systemFontOfSize:14.0]; NSDate *date = [managedObject valueForKey:@"lastmoddate"]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"EEE, MMM d, YYYY h:mm a"]; NSString *dateString = [formatter stringFromDate:date]; UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 215, 10)]; lblDate.text = dateString; lblDate.textColor = [UIColor grayColor]; lblDate.font = [UIFont systemFontOfSize:10.0]; [lblDate setBackgroundColor:[UIColor clearColor]]; [cell.contentView addSubview:lblDate]; return cell; } 

Here is the image:

enter image description here

+7
ios objective-c uitableview cell-formatting
source share
10 answers

This is what I came up with and it works well:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell==nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row]; NSString *entityName= [[managedObject entity]name]; NSDate *date = [managedObject valueForKey:@"lastmoddate"]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"EEE h:mm a MMM d, yy'''"]; NSString *dateString = [formatter stringFromDate:date]; UILabel *lblUser = [[UILabel alloc] initWithFrame:CGRectMake(30, 8, 215, 14)]; lblUser.text = [NSString stringWithFormat:@"%@ %i", entityName, [indexPath row]]; lblUser.textColor = [UIColor blackColor]; lblUser.font = [UIFont systemFontOfSize:16.0]; lblUser.tag = 1; [lblUser setBackgroundColor:[UIColor clearColor]]; UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(30, 21, 215, 20)]; lblDate.text = dateString; lblDate.textColor = [UIColor grayColor]; lblDate.font = [UIFont systemFontOfSize:12.0]; lblDate.tag = 2; [lblDate setBackgroundColor:[UIColor clearColor]]; if ((([cell.contentView viewWithTag:1]) && ([cell.contentView viewWithTag:2]))) { [[cell.contentView viewWithTag:1]removeFromSuperview]; [[cell.contentView viewWithTag:2]removeFromSuperview]; } [cell.contentView addSubview:lblDate]; [cell.contentView addSubview:lblUser]; return cell; } 
+6
source share

dequeueReusableCellWithIdentifier: forIndexPath: is guaranteed to return a cell (either a new one or one of the reuse queues), so the if (cell == nil) clause will never be executed - so it does not make a difference whether you delete the views or not. Labels overlap because this is the way you set it up. The default label is on the left side of the cell, and lblDate is also on the left (10 points on the left). Even if you move lblDate to the right, it may not be displayed, because I think the default label has the full width of the cell. It would be better to create a custom cell with two labels that you place where you want.

You also need to check if a label exists before adding another. You can give the labels a unique tag and check the presentation with that tag, or, most simply, I just want to create a custom cell in the storyboard or xib and add labels there. Then you only need to add content to them in the code.

+2
source share

try it

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; if (cell==nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 215, 10)]; [cell.contentView addSubview:lblDate]; } NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row]; NSString *entityName= [[managedObject entity]name]; cell.textLabel.text = [NSString stringWithFormat:@"%@ %i", entityName, [indexPath row]]; cell.textLabel.font=[UIFont systemFontOfSize:14.0]; lblDate.text = dateString; lblDate.textColor = [UIColor grayColor]; lblDate.font = [UIFont systemFontOfSize:10.0]; [lblDate setBackgroundColor:[UIColor clearColor]]; NSDate *date = [managedObject valueForKey:@"lastmoddate"]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"EEE, MMM d, YYYY h:mm a"]; NSString *dateString = [formatter stringFromDate:date]; return cell; } 
+2
source share

This is a problem with recreating cell contents. Try the following code segment.

 for(UIView *view in cell.contentView.subviews){ if ([view isKindOfClass:[UIView class]]) { [view removeFromSuperview]; } } 
+2
source share

Add this line:

 [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

Before:

 [cell.contentView addSubview:lblDate]; 
+2
source share

You are correct in writing code to remove all content from a cell subcategory. But you wrote it in the wrong place. UITableView dequeueReusableCellWithIdentifier will return you a cell after it is dequeueReusableCellWithIdentifier and initialized once. That way, the code you wrote to remove cell.contentView.subViews will never run, and you will get Overlapped views.

You can either fix this code in an else statement, but I do not prefer this. Why select and initialize all contentView every cell in a UITableView cell. Rather, I created UILabel once and gave it a tag to access it later. Like this:

  UILabel *lblDate = nil; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 215, 10)]; lblDate.textColor = [UIColor grayColor]; lblDate.font = [UIFont systemFontOfSize:10.0]; lblDate.tag = 1; [lblDate setBackgroundColor:[UIColor clearColor]]; [cell.contentView addSubview:lblDate]; } else { //get a reference to the label in the recycled view lblDate = (UILabel *)[cell.contentView viewWithTag:1]; } 
0
source share

Try this code. Your problem will be resolved.

 -(NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { return [arrTableData count]; } -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UILabel *lblName; UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; lblName = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 20.0)] autorelease]; lblName.tag = LBLNAME; lblName.numberOfLines=1; lblName.textAlignment=UITextAlignmentLeft; lblName.font = [UIFont systemFontOfSize:16.0]; lblName.textColor = [UIColor blackColor]; lblName.backgroundColor = [UIColor clearColor]; lblName.autoresizingMask = UIViewAutoresizingFlexibleRightMargin ; [cell.contentView addSubview:lblName]; }else{ lblName = (UILabel *)[cell.contentView viewWithTag:LBLNAME]; } if (arrTableData.count>0) { lblName.text=[NSString stringWithFormat:@"%@",[arrTableData objectAtIndex:indexPath.row]]; } return cell;} 
0
source share

try it

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row]; NSString *entityName= [[managedObject entity]name]; NSDate *date = [managedObject valueForKey:@"lastmoddate"]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"EEE h:mm a MMM d, yy'''"]; NSString *dateString = [formatter stringFromDate:date]; UILabel *lblUser = [[UILabel alloc] initWithFrame:CGRectMake(30, 8, 215, 14)]; lblUser.text = [NSString stringWithFormat:@"%@ %i", entityName, [indexPath row]]; lblUser.textColor = [UIColor blackColor]; lblUser.font = [UIFont systemFontOfSize:16.0]; lblUser.tag = 1; [lblUser setBackgroundColor:[UIColor clearColor]]; UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(30, 21, 215, 20)]; lblDate.text = dateString; lblDate.textColor = [UIColor grayColor]; lblDate.font = [UIFont systemFontOfSize:12.0]; lblDate.tag = 2; [lblDate setBackgroundColor:[UIColor clearColor]]; [cell.contentView addSubview:lblDate]; [cell.contentView addSubview:lblUser]; return cell; } 
0
source share
 if ([cell.contentView viewWithTag:tagnumber] { [[cell.contentView viewWithTag:tagnumber]removeFromSuperview]; } lblDate.tag = tagnumber; [cell.contentView addSubview:lblDate]; 

This line is simple enough to remove the previous tag subheadings and add a new subview with the tag .. Thanks for your answer

0
source share

In my case, the same problem occurred, allocating tableviewcell in 2 places, one of them is selected in the user class of tableviewcell, and the customview controller should be selected after I changed alllocation, then everything will work fine.

0
source share

All Articles