繁体   English   中英

UITableView单元格不显示标签和按钮

[英]UITableView cells don't show labels and buttons

我有一个UITableView,其委托和dataSource属性设置正确,但是未显示其内容。

我的密码

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

有人知道那里出什么事了吗? 提前致谢。

请注意 ,如果您设置了单元格的背景色,则该背景色看起来会有所不同,因此,这些单元格就在那里,并正确地使问题出在视图上

我不会说删除UITableViewCell的所有子视图并在其后添加自己的子视图是一个很好的做法。 最好将UITableViewCell子类化。

话虽这么说,您可以试试看,它将显示您的内容。

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

但是,您的问题肯定来自您要删除子视图的原因。 如果第一个问题是单元重叠,请尝试实施

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM