UITableView单元格重叠

Zac*_*c24 2 iphone objective-c uitableview ios

我正在实现一个带有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;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Fog*_*ter 7

他们没有重叠.每次使用单元格时都要添加其他标签.

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

图像视图等也是如此......

您最好的解决方案是使用自定义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;
}
Run Code Online (Sandbox Code Playgroud)

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