UITableViewCell 中的 UILabel 重叠

发布于 2024-12-22 02:10:49 字数 2826 浏览 0 评论 0原文

我有 2x UILabels,1 个标题 1 个副标题, 当选择segmentedControl 时,它们会发生变化。

它有效,但当选择不同的段时,我得到相同的 UILabel 重叠自身?

我想我需要创建一个操作来从超级视图中删除标签,然后再将其重新显示到单元格上?只是想知道如何去做

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

    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

I have a 2x UILabels, 1 title 1 subtitle,
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?

I think i need to create an action to remove the label from the superview before it is redisplayed onto the cell? just wondering how to go about it

2 Labels overlapping themselves

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

UPDATE:
Both were very valid answers, tyvm

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菩提树下叶撕阳。 2024-12-29 02:10:49

您没有正确地重用单元,因此您没有获得重用的性能优势,并使代码变得更加复杂。你也没有使用苹果为你提供的开箱即用的部件。

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

其次,UITableViewCell 已经内置了两个标签。您不需要创建它们。它们称为 textLabeldetailTextLabel。它们是普通的标签;您可以移动它们并使它们看起来像您想要的任何形状。在 cell==nil 块中执行此操作。

然后,您只需在 cell==nil 之外调用 cell.textLabel.text = ...cell.detailTextLabel.text = ... code> block 就可以了。

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

You're not reusing your cell correctly, so you're not getting the performance benefit of reuse, and making the code more complicated. You're also not using the pieces that Apple gives you to work with out of the box.

First, you should create and add all your subviews inside the cell==nil block. This is where you create your reusable cell. In the rest of the routine, you're just reconfiguring the cell. This is much, much faster.

Second, UITableViewCell already has two labels built-in. You don't need to create them. They're called textLabel and detailTextLabel. They're normal labels; you can move them around and make them look like whatever you want. Do that in the cell==nil block.

Then you just need to call cell.textLabel.text = ... and cell.detailTextLabel.text = ... outside the cell==nil block and you're good to go.

If you needed more labels than two, then I would subclass UITableViewCell and create new properties on it so that you can easily reconfigure the cell.

够钟 2024-12-29 02:10:49

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

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

I hope it will work now.Try this.....

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文