簡體   English   中英

UITableView 單元格重疊

[英]UITableView cell are overlapping

我正在實現一個帶有部分索引的 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;
}

通過這種方式,您只需在單元格中創建一個標簽和一個圖像視圖,然后重用它即可。

更改行UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; with 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;
}

那只是因為您在每個單元格數據源方法上創建新標簽只是更改

只需分配並添加標簽

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];

您正在添加cell.TextLabel和另一個標簽(名稱: user_name )。 如果你想要一個帶有圖像視圖和標簽的自定義表格單元格,你可以看到這個

正如您在輸出中看到的,標簽重疊。 它不是相同的標簽,它們是單元格中的不同標簽。 希望這可以幫助.. :)

編輯:試試這個:

- (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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM