UILabel在UITableViewCell中重叠

时间:2011-12-19 17:42:53

标签: iphone ios cocoa-touch uitableview uilabel

我有2个UILabels,1个标题1个副标题, 当选择segmentedControl时,这意味着要改变。

它有效,但是当选择了不同的片段时,我会让SAME UILabel重叠吗?

我想我需要创建一个动作,以便在重新显示到单元格之前从superview中删除标签?只是想知道如何去做它

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

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

2 个答案:

答案 0 :(得分:3)

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

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

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

然后你只需要在cell.textLabel.text = ...区块之外拨打cell.detailTextLabel.text = ...cell==nil,就可以了。

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

答案 1 :(得分:0)

我希望它现在能够奏效。试试这个......

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