UITableView单元格重叠

我正在实现一个带有section index的tableView。 每当行在任何特定部分中多于一个时,我的tableViewCells重叠。

这是我的代码。

![- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return \[self.keys count\]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { // return \[\[\[UILocalizedIndexedCollation currentCollation\] sectionTitles\] objectAtIndex:section\]; return \[keys objectAtIndex:section\]; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { // return \[\[UILocalizedIndexedCollation currentCollation\] sectionIndexTitles\]; return keys; } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return \[\[UILocalizedIndexedCollation currentCollation\] sectionForSectionIndexTitleAtIndex:index\]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = \[tableView dequeueReusableCellWithIdentifier:nil\]; if (cell == nil) { cell = \[\[UITableViewCell alloc\] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier\]; } \[self.exhibitorTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)\]; UIView* bgview = \[\[UIView alloc\] initWithFrame:CGRectMake(0, 0, 1, 1)\]; bgview.opaque = YES; bgview.backgroundColor = \[UIColor colorWithRed:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0\]; \[cell setBackgroundView:bgview\]; cell.textLabel.font = \[UIFont fontWithName:@"Roboto-Light" size:17\]; cell.detailTextLabel.font = \[UIFont fontWithName:@"Roboto-Light" size:13\]; cell.textLabel.backgroundColor = \[UIColor clearColor\]; cell.detailTextLabel.backgroundColor = \[UIColor clearColor\]; cell.textLabel.textColor = \[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0\]; UIImageView *defaultImage = \[\[UIImageView alloc\]init\]; \[defaultImage setFrame:CGRectMake(5.0f, 5.0f, 40.0f, 40.0f)\]; \[cell addSubview:defaultImage\]; for (int i=0; i<\[self.exhibitorArray count\]; i++) { NSString * string = \[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"name"\]; NSString *firstLetter = \[string substringToIndex:1\]; if (\[\[keys objectAtIndex:indexPath.section\] isEqualToString:firstLetter\] ) { NSString *urlString = \[\[NSString alloc\]init\]; urlString = @"http://"; urlString = \[urlString stringByAppendingString:\[NSString stringWithFormat:@"%@",\[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"image"\]\]\]; NSLog(@"%@",urlString); AsyncImageView *asyncImageView = \[\[AsyncImageView alloc\] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)\]; \[asyncImageView setBackgroundColor:\[UIColor clearColor\]\]; NSString *tmp_string = \[\[NSString alloc\]init\]; tmp_string = urlString; \[asyncImageView loadImageFromURL:\[NSURL URLWithString:tmp_string\]\]; \[defaultImage addSubview:asyncImageView\]; defaultImage.contentMode = UIViewContentModeScaleAspectFit; tmp_string = nil; asyncImageView = nil; defaultImage = nil; NSString *name_string = \[\[NSString alloc\]init\]; name_string = \[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"name"\]; NSLog(@"%@",name_string); UILabel *user_name = \[\[ UILabel alloc\]init \]; \[user_name setFrame:(CGRectMake(58, 5, 270, 25))\]; \[user_name setBackgroundColor: \[UIColor clearColor\]\]; \[user_name setText:name_string\]; \[user_name setFont:\[UIFont fontWithName:@"Roboto-Light" size:14\]\]; \[user_name setTextAlignment:NSTextAlignmentLeft\]; \[user_name setTextColor:\[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0\]\]; \[cell addSubview:user_name\]; user_name = nil; } } \[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator\]; return cell; } 

在此处输入图像描述

他们没有重叠。 每次使用单元格时都要添加一个附加标签。

您只需要向一个单元格添加一个标签,然后重复使用该标签,而不是每次都添加一个新标签。

对于图像视图等也是如此……

您最好的解决方案是使用自定义UITableViewCell子类… http://www.appcoda.com/customize-table-view-cells-for-uitableview/

可能的方案

注意:我不喜欢这样,因为使用了tag属性,但是如果没有子类化UITableviewCell就可以完成它…

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; UIImageView *defaultImageView; UILabel *customLabel; if (cell == nil) { // create the cell and empty views ready to take the content. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; defaultImageView = //create the image view defaultImageView.tag = 3; [cell.contentView addSubview:defaultImageView]; customLabel = //create custom label customLabel.tag = 4; [cell.contentView addSubview:customLabel]; } else { // get the views that have already been created defaultImageView = [cell.contentView viewWithTag:3]; customLabel = [cell.contentView viewWithTag:4]; } // now populate the views... defaultImageView.image = someImage; customLabel.text = @"Hello, world"; return cell; } 

这样,您只需在单元格中创建一个标签和一个图像视图,然后重新使用它。

这只是因为你在每个单元数据源方法上创建新标签只是改变

只需分配和添加标签

 if (cell == nil) { cell = \[\[UITableViewCell alloc\] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier\]; **//Allocate label here and set tag** UILabel *nameLabel = [[UILabel alloc] initwithframe]; [nameLabel setTag:212]; } **get label here by tag and set text on it here.** eg UILabel *nameLabel = [cell viewWithTag:212]; 

同样在条件内添加imageview并在外面设置图像

因为这行[cell addSubview:user_name]; 它已添加多次。要避免这种情况,请尝试以下操作:

 UILabel *user_name = [[ UILabel alloc]init ]; user_name.tag = SomeTagValue; . . . . UILabel *preView = [cell viewWithTag:SomeTagValue]; [preView removeFromSuperview]; [cell addSubview:user_name]; 

更改行UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; 使用UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

编辑

  - (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]; UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; bgview.opaque = YES; bgview.tag = 8888; bgview.backgroundColor = [UIColor colorWithRed:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0]; [cell setBackgroundView:bgview]; cell.textLabel.font = [UIFont fontWithName:@"Roboto-Light" size:17]; cell.detailTextLabel.font = [UIFont fontWithName:@"Roboto-Light" size:13]; cell.textLabel.backgroundColor = [UIColor clearColor]; cell.detailTextLabel.backgroundColor = [UIColor clearColor]; cell.textLabel.textColor = [UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0]; UIImageView *defaultImage = [[UIImageView alloc]init]; [defaultImage setFrame:CGRectMake(5.0f, 5.0f, 40.0f, 40.0f)]; defaultImage.contentMode = UIViewContentModeScaleAspectFit; defaultImage.tag = 7777; [cell.contentView addSubview:defaultImage]; UILabel *user_name = [[ UILabel alloc]init ]; user_name.tag = 9999; [user_name setFrame:(CGRectMake(58, 5, 270, 25))]; [user_name setBackgroundColor: [UIColor clearColor]]; [user_name setFont:[UIFont fontWithName:@"Roboto-Light" size:14]]; [user_name setTextAlignment:NSTextAlignmentLeft]; [user_name setTextColor:[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0]]; [cell.contentView addSubview:user_name]; AsyncImageView *asyncImageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)]; asyncImageView.tag = 6666; [asyncImageView setBackgroundColor:[UIColor clearColor]]; [defaultImage addSubview:asyncImageView]; } [self.exhibitorTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)]; UIImageView *defaultImage = (UIImageView*)[cell.contentView viewWithTag:7777]; UILabel *user_name = (UILabel*)[cell.contentView viewWithTag:9999]; AsyncImageView *asyncImageView = (AsyncImageView*)[defaultImage viewWithTag:6666]; for (int i=0; i<[self.exhibitorArray count]; i++) { NSString * string = [[self.exhibitorArray objectAtIndex:i]valueForKey:@"name"]; NSString *firstLetter = [string substringToIndex:1]; if ([[keys objectAtIndex:indexPath.section] isEqualToString:firstLetter] ) { NSString *urlString ; urlString = @"http://"; urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"%@",[[self.exhibitorArray objectAtIndex:i]valueForKey:@"image"]]]; NSLog(@"%@",urlString); NSString *tmp_string = urlString; [asyncImageView loadImageFromURL:[NSURL URLWithString:tmp_string]]; NSString *name_string = [[self.exhibitorArray objectAtIndex:i]valueForKey:@"name"]; NSLog(@"%@",name_string); [user_name setText:name_string]; } } [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; return cell; } 

您正在添加cell.TextLabel和另一个Label(Named: user_name )。 如果您想要一个带有imageview和标签的自定义表格单元格,您可以看到这一点 。

正如您在输出中看到的那样,标签重叠。 它们不是同一个标签,它们是一个单元格中的不同标签。 希望这可以帮助.. :)

编辑:试试这个:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = \[tableView dequeueReusableCellWithIdentifier:nil\]; if (cell == nil) { cell = \[\[UITableViewCell alloc\] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier\]; } \[self.exhibitorTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)\]; UIView* bgview = \[\[UIView alloc\] initWithFrame:CGRectMake(0, 0, 1, 1)\]; bgview.opaque = YES; bgview.backgroundColor = \[UIColor colorWithRed:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0\]; \[cell setBackgroundView:bgview\]; UIImageView *defaultImage = \[\[UIImageView alloc\]init\]; \[defaultImage setFrame:CGRectMake(5.0f, 5.0f, 40.0f, 40.0f)\]; \[cell addSubview:defaultImage\]; for (int i=0; i<\[self.exhibitorArray count\]; i++) { NSString * string = \[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"name"\]; NSString *firstLetter = \[string substringToIndex:1\]; if (\[\[keys objectAtIndex:indexPath.section\] isEqualToString:firstLetter\] ) { NSString *urlString = \[\[NSString alloc\]init\]; urlString = @"http://"; urlString = \[urlString stringByAppendingString:\[NSString stringWithFormat:@"%@",\[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"image"\]\]\]; NSLog(@"%@",urlString); AsyncImageView *asyncImageView = \[\[AsyncImageView alloc\] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)\]; \[asyncImageView setBackgroundColor:\[UIColor clearColor\]\]; NSString *tmp_string = \[\[NSString alloc\]init\]; tmp_string = urlString; \[asyncImageView loadImageFromURL:\[NSURL URLWithString:tmp_string\]\]; \[defaultImage addSubview:asyncImageView\]; defaultImage.contentMode = UIViewContentModeScaleAspectFit; tmp_string = nil; asyncImageView = nil; defaultImage = nil; NSString *name_string = \[\[NSString alloc\]init\]; name_string = \[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"name"\]; NSLog(@"%@",name_string); UILabel *user_name = \[\[ UILabel alloc\]init \]; \[user_name setFrame:(CGRectMake(58, 5, 270, 25))\]; \[user_name setBackgroundColor: \[UIColor clearColor\]\]; \[user_name setText:name_string\]; \[user_name setFont:\[UIFont fontWithName:@"Roboto-Light" size:14\]\]; \[user_name setTextAlignment:NSTextAlignmentLeft\]; \[user_name setTextColor:\[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0\]\]; \[cell addSubview:user_name\]; user_name = nil; } } \[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator\]; return cell; }