Ios UITableView单元格不';不显示标签和按钮
我有一个UITableView,它的委托和数据源属性设置正确,但是它的内容没有显示出来 我的代码Ios UITableView单元格不';不显示标签和按钮,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,我有一个UITableView,它的委托和数据源属性设置正确,但是它的内容没有显示出来 我的代码 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ BOOL notInputNotes; UIColor *normalBlue = [[UIColor alloc]initWithRed:0.1 green:0.65 blue:
-(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*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
布尔不输入注释;
UIColor*normalBlue=[[UIColor alloc]initWithRed:0.1绿色:0.65蓝色:1.0阿尔法:0.8];
作业*hw=[hwArray objectAtIndex:indexath.row];
NSString*DateDueAstText=[dateFormat stringFromDate:hw.dateDue];
cgordate;
如果(DateDueAstText.length<16 | | DateDueAstText.length==16){
xForDate=215;
}
否则{
xForDate=214;
}
hw.indepath=indepath;
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
用于(UIView*单元格中的视图。子视图){
[视图从SuperView移除];
}
如果(!i编辑){
cell.selectionStyle=UITableViewCellSelectionStyleNone;
[单元格设置编辑:否];
}
否则{
cell.selectionStyle=UITableViewCellSelectionStyleDefault;
[单元格设置编辑:是];
}
UILabel*subject=[[UILabel alloc]initWithFrame:CGRectMake(70,5150,45)];
如果(![hw.subject IsequalString:@“”)
{
subject.text=hw.subject;
notInputNotes=否;
}
否则{
subject.text=hw.notes;
subject.frame=CGRectMake(70,20,150,45);
notInputNotes=是;
}
[主题集字体:[UIFont systemFontOfSize:20]];
[主题设置颜色:正常蓝色];
subject.adjustsFontSizeToFitWidth=是;
subject.backgroundColor=[UIColor blueColor];//测试
[单元格添加子视图:主题];
如果(!notInputNotes){
UILabel*notes=[[UILabel alloc]initWithFrame:CGRectMake(70,40,300,30)];
notes.text=hw.notes;
[notes setFont:[UIFont systemFontOfSize:12];
notes.adjustsFontSizeToFitWidth=是;
[notes setTextColor:[UIColor COLOR WITH RED:0.2绿色:0.3蓝色:1.0阿尔法:1.0];
[单元格添加子视图:注释];
}
UILabel*重要性=[[UILabel alloc]initWithFrame:CGRectMake(300,45,200,30)];
importancey.text=hw.importancey;
[重要性设置字体:[UIFont systemFontOfSize:12]];
[重要性设置xtColor:[UIColor REDCLOR]];
重要性.adjustsFontSizeToFitWidth=是;
[单元添加子视图:重要性];
UILabel*dateDue=[[UILabel alloc]initWithFrame:CGRectMake(xForDate,10200,30)];
dateDue.text=[NSString stringWithFormat:@“%@”,DateDueAstText];
[dateDue setFont:[UIFont systemFontOfSize:13];
dateDue.textColor=normalBlue;
dateDue.adjustsFontSizeToFitWidth=是;
[单元格添加子视图:dateDue];
hw.doneButton=[[UIButton alloc]initWithFrame:CGRectMake(20,25,40,40)];
如果(!硬件完成){
[hw.doneButton setImage:[UIImage ImageName:@“unchecked.png”]用于状态:UIControlStateNormal];
}
否则{
[hw.doneButton setImage:[UIImage ImageName:@“checked.png”]用于状态:UIControlStateNormal];
}
[hw.doneButton addTarget:hw action:@selector(done:)for controlEvents:UIControlEventTouchUpInside];
NSDateComponents*dayComponent=[[NSDateComponents alloc]init];
dayComponent.day=-1;
NSCalendar*日历=[NSCalendar当前日历];
NSDate*NowMins1=[Calendar date通过添加组件:dayComponent toDate:[NSDate date]选项:0];
比较结果;
结果=[nowmus1比较:hw.dateDue];
如果(结果==取消搜索&&!(硬件完成)){
cell.backgroundColor=[UIColor colorWithWhite:0.86 alpha:0.6];
NSLog(@“%@早于%@”,dateDueAsText[dateFormat stringFromDate:NowMins1]);
[hw.doneButton setImage:[UIImage imageNamed:@“lighter-gray_unkecked.jpg”]用于状态:uicontrolstateformal];
hw.late=是;
}
否则{
cell.backgroundColor=[UIColor whiteColor];
hw.late=否;
}
[单元添加子视图:hw.doneButton];
返回单元;
}
有人知道那里出了什么事吗?提前谢谢
注意
如果设置单元格,其背景颜色会有所不同,因此单元格就在那里,并正确地重新生成。问题确实出在视图上我不认为删除UITableViewCell的所有子视图并在之后添加自己的子视图是一种很好的做法。最好将UITableViewCell子类化 也就是说,你可以试试这个,它会显示你的内容
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
}
但是您的问题肯定来自您想要删除子视图。如果第一个问题是单元重叠,请尝试实现
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return customHeight;
}
为什么要从单元子视图中删除UIView?此外,您还将单元格样式指定为默认样式,并向其中添加自定义UILabel。如果需要创建自定义单元格,可以使用故事板执行相同操作。@user2071152我正在从子视图中删除UIView,因为如果不这样做,单元格将彼此重叠哦,天哪!。在我的一生中,我从来没有对
cellforrowatinex
方法施加过如此沉重的负担。尝试将子视图添加到单元格。contentView
。另外,为什么不为此创建一个自定义单元格呢。代码将非常可读