Ios 我的uitableviewCell的内容没有';你不来吗?
我不知道为什么我的单元格内容没有显示在我的tableview控制器中,即使我用NSLog检查是否有值,我找到了它们,但没有显示Ios 我的uitableviewCell的内容没有';你不来吗?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我不知道为什么我的单元格内容没有显示在我的tableview控制器中,即使我用NSLog检查是否有值,我找到了它们,但没有显示 我以编程方式创建了我的单元格,也创建了我的视图 那些是文件 单元格.h: #import <UIKit/UIKit.h> @interface ContactTableViewCell : UITableViewCell @property (strong, nonatomic) UIImage *contactImage; @property (str
我以编程方式创建了我的单元格,也创建了我的视图
那些是文件
单元格.h:
#import <UIKit/UIKit.h>
@interface ContactTableViewCell : UITableViewCell
@property (strong, nonatomic) UIImage *contactImage;
@property (strong, nonatomic) UILabel *fullNameLabel;
@property (strong, nonatomic) UILabel *phoneNumberLabel;
@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
#导入“ContactsTableView.h”
#导入“ContactTableViewCell.h”
@接口联系人StableView()
@性质(强、非原子)NSArray*含量;
@结束
@实现联系人StableView
-(instancetype)initWithContent:(NSArray*)内容{
self=[super initWithFrame:self.bounds样式:UITableViewStylePlain];
self.delegate=self;
self.dataSource=self;
self.content=内容;
回归自我;
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回_content.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString*cellIdentifier=@“CustomCell”;
ContactTableViewCell*单元格=(ContactTableViewCell*)[tableView出列可重用CellWithIdentifier:cellIdentifier];
如果(单元格==nil)
{
cell=[[ContactTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellIdentifier];
}
cell.backgroundColor=[UIColor colorWithRed:249.0/255 green:237.0/255 blue:224.0/255 alpha:1.0];
cell.fullnamelab.text=self.content[indexPath.row];
cell.phoneNumberLabel.text=self.content[indexPath.row];
[细胞更新应变];
返回单元;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath;
{
NSLog(@“单元格%ld的标题”,indexath.row);
}
@结束
这是我将单元格设置为红色时的背景:
我在上面评论的帮助下发现了问题,问题是
updateViewConstraints
实际上没有执行!因此,我所要做的就是在创建组件后将其添加到我的
手机
这就是工作当您在调试中单步执行时,您会看到什么?
initWithStyle
是否实际被调用?如果是,子视图是否真的添加到单元格的contentView中?如果是,约束设置是否正确?需要比“它们没有显示”更详细一点的信息……对于initWithStyle,当我使用调试器检查时,它被完美地调用了,实际上我确实用代码“[self.contentView addSubview:self.fullnamelab]的一部分将内容添加到了单元格的contentView中;对于自动布局,我认为它们设置正确添加一行以获取didSelectRowAtIndexPath
中的当前单元格设置断点…使用调试控制台检查子视图的存在和/或帧…或者,使用调试层次结构
查找它们的位置。好的,我按照您的指示执行了操作,并检查了单击的单元格是否符合tains它的内容,我发现它们,所以可能是我的约束有问题,我会检查它们now@SAM你可以拍一张快照,然后展示给我们看。
@interface ContactsTableView : UITableView
- (instancetype) initWithContent : (NSArray *) content;
@end
#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
[self setNeedsUpdateConstraints];
[self updateViewConstraints];