将子视图添加到UITableViewCell

时间:2013-12-07 17:42:37

标签: ios objective-c uitableview ios7

我必须在UITableViewCell中添加一些标签,但每行中有不同数量的标签。我正在尝试在每个单元格中添加子视图,但是当我滚动所有标签重叠时。

我使用此代码获得了我想要的标签数量

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

}

结果是这张图片: enter image description here 有什么方法可以正确地做到这一点?我必须在哪里制作addSubview?

谢谢!

1 个答案:

答案 0 :(得分:4)

我想只有在滚动多个TableView高度后才会出现此问题!? 问题是细胞被重复使用。因此,当您向重用的单元格添加新标签时,旧的标签仍然存在。在添加新的子视图之前,您需要从contentView中删除所有子视图。 如果您有自定义UITableViewCell类,则可以在prepareForReuse()方法中执行此操作。