0
我不知道爲什麼我的單元格的內容不會顯示在我的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
你看到了什麼,當你通過步在調試?實際上調用了'initWithStyle'嗎?如果是這樣,子視圖是否真的被添加到單元格的contentView?如果是這樣,約束是否正確設置?需要比「他們沒有出現」更詳細的信息... – DonMag
for ** initWithStyle **當我使用調試器檢查它完全被調用時,實際上我確實將內容添加到了代碼部分的單元格contentView中「[self.contentView addSubview:self.fullNameLabel]; for autolayout我認爲它們設置正確 – SAM
添加一行以獲取'didSelectRowAtIndexPath'中的當前單元格...設置斷點......使用調試控制檯檢查子視圖的存在和/或框架......或者,使用'Debug Hierarchy'來找出他們去哪裏。 – DonMag