UITableViewCell 中的 UILabel 重叠

Posted

技术标签:

【中文标题】UITableViewCell 中的 UILabel 重叠【英文标题】:UILabel overlapping in UITableViewCell 【发布时间】:2011-12-19 17:42:53 【问题描述】:

我有 2 个 UILabel,1 个标题 1 个副标题, Which are meant to change when a segmentedControl is selected.

It works but instead i get the SAME UILabel overlapping itself when a different segment is selected?

我认为我需要创建一个操作以在将标签重新显示到单元格之前从超级视图中删除它?只是想知道如何去做

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    

    EventUpcoming *aEventUpcoming = [euEvent objectAtIndex:indexPath.section];
    EventWeekly *aEventWeekly = [ewEvent objectAtIndex:indexPath.section];

    UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 290, 20)];
    UILabel *cellSubtitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 290, 20)];

    NSString *titleString = [[NSString alloc] init];
    NSString *subtitleString = [[NSString alloc] init];

    if (segmentedControl.selectedSegmentIndex == 0) 
    
        Event *aEvent = [aEventUpcoming.event objectAtIndex:indexPath.row];
        titleString = aEvent.name;
        subtitleString = aEvent.subtitle;
     
    else 
    
        Event *aEvent = [aEventWeekly.event objectAtIndex:indexPath.row];
        titleString = aEvent.name;
        subtitleString = aEvent.subtitle;
    

    NSString *titleStringUC = [titleString uppercaseString];
    NSString *subtitleStringLC = [subtitleString lowercaseString];

    cellTitle.text = titleStringUC;
    cellTitle.font = [UIFont boldSystemFontOfSize:11];
    cellTitle.textColor = [UIColor colorWithRed:142/255.0f green:142/255.0f blue:142/255.0f alpha:1];
    cellTitle.shadowColor = [UIColor whiteColor];
    cellTitle.shadowOffset = CGSizeMake(1, 1);
    cellTitle.backgroundColor = [UIColor clearColor];

    cellSubtitle.text = subtitleStringLC;
    cellSubtitle.font = [UIFont boldSystemFontOfSize:11];
    cellSubtitle.textColor = [UIColor colorWithRed:177/255.0f green:177/255.0f blue:177/255.0f alpha:1];
    cellSubtitle.shadowColor = [UIColor whiteColor];
    cellSubtitle.shadowOffset = CGSizeMake(1, 1);
    cellSubtitle.backgroundColor = [UIColor clearColor];


    tableViewCellSeparator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCellSeparator.png"]];
    tableViewCellSeparator.frame = CGRectMake(0, cell.bounds.size.height - 2, 320, 2);

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    [cell.contentView addSubview:cellTitle];
    [cell.contentView addSubview:cellSubtitle];
    [cell.contentView addSubview:tableViewCellSeparator];






    return cell;


更新: 两者都是非常有效的答案,tyvm

【问题讨论】:

【参考方案1】:

您没有正确重用您的单元格,因此您没有获得重用的性能优势,并使代码更加复杂。您也没有使用 Apple 提供的开箱即用的组件。

首先,您应该在cell==nil 块中创建并添加所有子视图。这是您创建可重复使用单元的地方。在其余的例程中,您只是重新配置单元。这要快得多。

其次,UITableViewCell 已经内置了两个标签。您不需要创建它们。它们被称为textLabeldetailTextLabel。它们是正常的标签;你可以移动它们,让它们看起来像你想要的任何东西。在cell==nil 块中执行此操作。

那么你只需要在cell==nil 块之外调用cell.textLabel.text = ...cell.detailTextLabel.text = ... 就可以了。

如果您需要两个以上的标签,那么我将继承 UITableViewCell 并在其上创建新属性,以便您可以轻松地重新配置单元格。

【讨论】:

【参考方案2】:

我希望它现在可以工作。试试这个.....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    EventUpcoming *aEventUpcoming = [euEvent objectAtIndex:indexPath.section];
    EventWeekly *aEventWeekly = [ewEvent objectAtIndex:indexPath.section];

    UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 290, 20)];
    UILabel *cellSubtitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 290, 20)];

    NSString *titleString = [[NSString alloc] init];
    NSString *subtitleString = [[NSString alloc] init];

    NSString *titleStringUC = [titleString uppercaseString];
    NSString *subtitleStringLC = [subtitleString lowercaseString];

    cellTitle.text = titleStringUC;
    cellTitle.font = [UIFont boldSystemFontOfSize:11];
    cellTitle.textColor = [UIColor colorWithRed:142/255.0f green:142/255.0f blue:142/255.0f alpha:1];
    cellTitle.shadowColor = [UIColor whiteColor];
    cellTitle.shadowOffset = CGSizeMake(1, 1);
    cellTitle.backgroundColor = [UIColor clearColor];
    cellTitle.tag = 10;

    cellSubtitle.text = subtitleStringLC;
    cellSubtitle.font = [UIFont boldSystemFontOfSize:11];
    cellSubtitle.textColor = [UIColor colorWithRed:177/255.0f green:177/255.0f blue:177/255.0f alpha:1];
    cellSubtitle.shadowColor = [UIColor whiteColor];
    cellSubtitle.shadowOffset = CGSizeMake(1, 1);
    cellSubtitle.backgroundColor = [UIColor clearColor];
    cellSubtitle.tag = 11;


    tableViewCellSeparator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TableCellSeparator.png"]];
    tableViewCellSeparator.frame = CGRectMake(0, cell.bounds.size.height - 2, 320, 2);

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


    [cell.contentView addSubview:cellTitle];
    [cell.contentView addSubview:cellSubtitle];
    [cell.contentView addSubview:tableViewCellSeparator];


cellTitle = (UILabel *)[cell.contentView viewWithTag:10];
 cellSubtitle = (UILabel *)[cell.contentView viewWithTag:11];

if (segmentedControl.selectedSegmentIndex == 0) 

    Event *aEvent = [aEventUpcoming.event objectAtIndex:indexPath.row];
    titleString = aEvent.name;
    subtitleString = aEvent.subtitle;
 
else 

    Event *aEvent = [aEventWeekly.event objectAtIndex:indexPath.row];
    titleString = aEvent.name;
    subtitleString = aEvent.subtitle;


return cell;

【讨论】:

以上是关于UITableViewCell 中的 UILabel 重叠的主要内容,如果未能解决你的问题,请参考以下文章

更改 UITableViewCell 中的对象也更改了重用 UITableViewCell 的对象

确定 UITableViewCell 中的 UIButton

UITableViewCell 事件中的 UIButton 不起作用

UITableViewCell 中的 UICollectionView

UITableVIewCell 中的 UIScrollView 不会滚动

UITableViewCell 中的 UITextField - UIButton