0

I have to put some labels into a UITableViewCell but there are different number of labels in each row. I'm trying to add a subview in each cell but when I do scroll all the labels overlap .

I have a for with the number of labels I want with this code

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

{

NSString *CellIdentifier = @"dayCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];    

UILabel* titleLabel = (UILabel*)[cell viewWithTag:100];
NSString * stringDate = [NSString stringWithFormat:@"%@ %@",[[days objectAtIndex:indexPath.row] substringWithRange:NSMakeRange(8, 2)], [self getNameDay:[arrayOfWeek objectAtIndex:indexPath.row]]];
titleLabel.text = stringDate;
titleLabel.textColor = [UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0];

if ([keys containsObject:[days objectAtIndex:indexPath.row]]) {
    titleLabel.font = [UIFont fontWithName:@"Roboto-Bold" size:15.0];
    [cell.contentView.superview setClipsToBounds:NO];

    NSDictionary * eventConcret = [events objectForKey:[days objectAtIndex:indexPath.row]];
    NSArray * keysEventConcret = [eventConcret allKeys];
    CGRect frame = titleLabel.frame;

    for (int i=0; i<[keysEventConcret count]; i++) {

        for (int j=0; j<[[eventConcret objectForKey:[keysEventConcret objectAtIndex:i]] count]; j++) {

            frame.origin.y += 35;
            UILabel *hourEvent = [[UILabel alloc] initWithFrame:frame];
            hourEvent.text =  [NSString stringWithFormat: [self getHourStringFromDate:[[[eventConcret objectForKey:[keysEventConcret objectAtIndex:i]] objectAtIndex:j] objectForKey:@"event_start"]]];
            hourEvent.backgroundColor = [UIColor clearColor];
            hourEvent.font=[UIFont fontWithName:@"Roboto-Regular" size:15];
            hourEvent.textColor = [UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0];
            hourEvent.tag = 3;
            [cell.contentView addSubview:hourEvent];

            frame.origin.y += 25;
            UILabel *contentEvent = [[UILabel alloc] initWithFrame:frame];
            contentEvent.text = [[[eventConcret objectForKey:[keysEventConcret objectAtIndex:i]] objectAtIndex:j] objectForKey:@"event_title"];
            [contentEvent setNumberOfLines:0];
            contentEvent.backgroundColor = [UIColor clearColor];
            contentEvent.font=[UIFont fontWithName:@"Roboto-Regular" size:15];
            contentEvent.textColor = [UIColor colorWithRed:89.0/255.0 green:187.0/255.0 blue:98.0/255.0 alpha:1.0];
            contentEvent.tag = 4;

            [cell addSubview:contentEvent];

            UIButton *buttonContent = [UIButton buttonWithType:UIButtonTypeCustom];
            buttonContent.frame = frame;
            buttonContent.backgroundColor = [UIColor clearColor];
            buttonContent.titleLabel.text = [keysEventConcret objectAtIndex:i];
            buttonContent.titleLabel.textColor = [UIColor clearColor];
            buttonContent.tag = indexPath.row+j*1000;
            [buttonContent addTarget:self
                              action:@selector(chooseEventButton:)
                    forControlEvents:UIControlEventTouchDown];

            [cell.contentView addSubview:buttonContent];

        }

    }

}
else{
    titleLabel.font = [UIFont fontWithName:@"Roboto-Light" size:15.0];

    }

return cell;

}

The result is this image: enter image description here What's the way to do it correctly? Where I have to make the addSubview?

Thanks!

2
  • show the entire code of cellForRowAtIndexPath, your problem is that your cell is reusable
    – Ilario
    Dec 7, 2013 at 17:46
  • Use storyboard. You can do easily in storyboard with add UITableViewCell
    – SukruK
    Dec 7, 2013 at 22:23

1 Answer 1

4

I guess this problem appears only after you scroll more than one TableView height!? The problem is that cells are reused. So when you add new labels to a reused cell the old ones are still there. You need to remove all subviews from contentView before adding the new ones. If you have a custom UITableViewCell class you can do so in prepareForReuse() method.

1
  • Problem solved: I remove all subviews in every iteration! NSArray* subviews = [cell.contentView subviews]; for (UIView* subview in subviews) { if (subview.tag==100) { //res } else{ [subview removeFromSuperview]; } } Dec 7, 2013 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.