Andyy
Andyy

Reputation: 495

UILabel overlapping in UITableViewCell

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

Upvotes: 1

Views: 1856

Answers (2)

Emon
Emon

Reputation: 958

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

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299275

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.

Upvotes: 3

Related Questions