UITableView cells don't show labels and buttons

I have a UITableView that has its delegate and dataSource properties set correctly, however its content isn't showing.

MY Code

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

    BOOL notInputNotes;
    UIColor *normalBlue = [[UIColor alloc]initWithRed:0.1 green:0.65 blue:1.0 alpha:0.8];
    HomeWork *hw = [hwArray objectAtIndex:indexPath.row];
    NSString *dateDueAsText = [dateFormat stringFromDate:hw.dateDue];
    CGFloat xForDate;
    if (dateDueAsText.length < 16 || dateDueAsText.length == 16) {
        xForDate = 215;
    }
    else{
        xForDate = 214;
    }
    hw.indexPath = indexPath;
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    for (UIView *view in cell.subviews) {
        [view removeFromSuperview];
    }
    if (!isEditing) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setEditing:NO];
    }
    else{
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
        [cell setEditing:YES];
    }
    UILabel *subject = [[UILabel alloc]initWithFrame:CGRectMake(70, 5, 150, 45)];
    if(![hw.subject isEqualToString:@""])
    {
        subject.text = hw.subject;
        notInputNotes = NO;
    }
    else{
        subject.text = hw.notes;
        subject.frame = CGRectMake(70, 20, 150, 45);
        notInputNotes = YES;
    }
    [subject setFont:[UIFont systemFontOfSize:20]];
    [subject setTextColor:normalBlue];
    subject.adjustsFontSizeToFitWidth = YES;
    subject.backgroundColor = [UIColor blueColor];//test
    [cell addSubview:subject];
    if(!notInputNotes){
        UILabel *notes = [[UILabel alloc]initWithFrame:CGRectMake(70, 40, 300, 30)];
        notes.text = hw.notes;
        [notes setFont:[UIFont systemFontOfSize:12]];
        notes.adjustsFontSizeToFitWidth = YES;
        [notes setTextColor:[UIColor colorWithRed:0.2 green:0.3 blue:1.0 alpha:1.0]];
        [cell addSubview:notes];
    }
    UILabel *importancy = [[UILabel alloc]initWithFrame:CGRectMake(300, 45, 200, 30)];
    importancy.text = hw.importancy;
    [importancy setFont:[UIFont systemFontOfSize:12]];
    [importancy setTextColor:[UIColor redColor]];
    importancy.adjustsFontSizeToFitWidth = YES;
    [cell addSubview:importancy];
    UILabel *dateDue = [[UILabel alloc]initWithFrame:CGRectMake(xForDate, 10, 200, 30)];
    dateDue.text = [NSString stringWithFormat:@"%@",dateDueAsText];
    [dateDue setFont:[UIFont systemFontOfSize:13]];
    dateDue.textColor = normalBlue;
    dateDue.adjustsFontSizeToFitWidth = YES;
    [cell addSubview:dateDue];
    hw.doneButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)];
        if(!hw.done){
            [hw.doneButton setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
        }
        else{
            [hw.doneButton setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
        }
    [hw.doneButton addTarget:hw action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];
    NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
    dayComponent.day = -1;
    NSCalendar *theCalendar = [NSCalendar currentCalendar];
    NSDate *nowMinus1 = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];
    NSComparisonResult result;
    result = [nowMinus1 compare:hw.dateDue];
    if(result==NSOrderedDescending && !(hw.done)){
        cell.backgroundColor = [UIColor colorWithWhite:0.86 alpha:0.6];
        NSLog(@"%@ is earlier than %@",dateDueAsText,[dateFormat stringFromDate:nowMinus1]);
            [hw.doneButton setImage:[UIImage imageNamed:@"lighter-gray_unckecked.jpg"] forState:UIControlStateNormal];
        hw.late = YES;
    }
    else{
        cell.backgroundColor = [UIColor whiteColor];
        hw.late = NO;
    }
    [cell addSubview:hw.doneButton];
    return cell;
}

Does somebody know what's wrong there? Thanks in advance.

Note the cell's background color appears different if you set it, so the cells are there and re correctly made the problem is really with the views

Answers


I wouldn't say it is a really good practice to remove all subviews of a UITableViewCell and add your own subviews after. It would be better to subclass UITableViewCell.

That being said, you can try this instead, it will display your content.

for (UIView *view in cell.contentView.subviews) {
    [view removeFromSuperview];
}

But your problem definitely comes from you wanting to remove the subviews. If the first problem is cell overlapping try implementing

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return customHeight;
}

Need Your Help

How do I check if a longitude/latitude point is within a range of coordinates?

php math geolocation coordinates latitude-longitude

I have a number of longitude and latitude coordinates that make up a polygonal zone. I also have a longitude and latitude coordinate to define the position of a vehicle. How do I check that the veh...

How to display mask in text input control

javascript jquery html css

How to make such a thing happen so that the input box shows underscore/underline right when you click the input box and then as you type the dashes keep getting replaced by characters.