我的uitableviewCell的内容不会显示?
问题描述:
我不知道为什么我的单元格的内容不会显示在我的tableview控制器中,即使我检查是否存在使用NSLog的值,但我找不到它们!
我已经建立了我的细胞编程也是我的观点
那些文件
cell.h:我的uitableviewCell的内容不会显示?
#import <UIKit/UIKit.h>
@interface ContactTableViewCell : UITableViewCell
@property (strong, nonatomic) UIImage *contactImage;
@property (strong, nonatomic) UILabel *fullNameLabel;
@property (strong, nonatomic) UILabel *phoneNumberLabel;
@end
cell.m
#import "ContactTableViewCell.h"
#import <Masonry.h>
@interface ContactTableViewCell()
@end
@implementation ContactTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.fullNameLabel = [[UILabel alloc] init];
[self.fullNameLabel setTextColor:[UIColor redColor]];
[self.fullNameLabel setBackgroundColor:[UIColor blueColor]];
[self.fullNameLabel setHidden:NO];
self.fullNameLabel.text [email protected]"test";
[self.contentView addSubview:self.fullNameLabel];
// Add this label to the button
self.phoneNumberLabel = [[UILabel alloc] init];
[self.phoneNumberLabel setNumberOfLines:0];
[self.phoneNumberLabel setTextColor:[UIColor redColor]];
[self.phoneNumberLabel setBackgroundColor:[UIColor blueColor]];
[self.contentView addSubview:_fullNameLabel];
[self.phoneNumberLabel setHidden:NO];
}
return self;
}
// tell UIKit that you are using AutoLayout
+ (BOOL)requiresConstraintBasedLayout {
return YES;
}
-(void)updateViewConstraints{
[_fullNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.contentView.mas_top);
make.right.equalTo(self.contentView.mas_right);
make.left.equalTo(self.contentView.mas_left);
make.bottom.equalTo(self.contentView.mas_bottom);
}];
[self updateConstraints];
}
@end
uitableview.h
@interface ContactsTableView : UITableView
- (instancetype) initWithContent : (NSArray *) content;
@end
UitableView.m
#import "ContactsTableView.h"
#import "ContactTableViewCell.h"
@interface ContactsTableView() <UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic) NSArray *content;
@end
@implementation ContactsTableView
- (instancetype) initWithContent : (NSArray *) content {
self = [super initWithFrame:self.bounds style:UITableViewStylePlain];
self.delegate = self;
self.dataSource = self;
self.content = content;
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _content.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"CustomCell";
ContactTableViewCell *cell = (ContactTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[ContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.backgroundColor = [UIColor colorWithRed:249.0/255 green:237.0/255 blue:224.0/255 alpha:1.0];
cell.fullNameLabel.text = self.content[indexPath.row];
cell.phoneNumberLabel.text = self.content[indexPath.row];
[cell updateConstraints];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSLog(@"title of cell %ld", indexPath.row);
}
@end
这是单元格的背景,当我把它冲: enter image description here
答
我发现人的意见的帮助下问题以上,问题是the updateViewConstraints
没有实际执行!所以我不得不这样做是我的
cell.m
[self setNeedsUpdateConstraints];
[self updateViewConstraints];
创建我的部件这对我的代码之后添加和它的工作
你看到了什么,当你通过步在调试?实际上调用了'initWithStyle'吗?如果是这样,子视图是否真的被添加到单元格的contentView?如果是这样,约束是否正确设置?需要比“他们没有出现”更详细的信息... – DonMag
for ** initWithStyle **当我使用调试器检查它完全被调用时,实际上我确实将内容添加到了代码部分的单元格contentView中“[self.contentView addSubview:self.fullNameLabel]; for autolayout我认为它们设置正确 – SAM
添加一行以获取'didSelectRowAtIndexPath'中的当前单元格...设置断点......使用调试控制台检查子视图的存在和/或框架......或者,使用'Debug Hierarchy'来找出他们去哪里。 – DonMag