[英]How to load UItableList Programatically on UIview xib files
嗨,我是iOS的新手,在我的应用程序中,我以编程方式在UIView xib文件上加载UITableView,但使用下面的代码UItableList不在我的UIView xib文件上添加了我在这里做错了什么,请帮助我
#import "FaqQuestionsClass.h"
@implementation FaqQuestionsClass
{
BackGroundClass * Bg;
UITableView * tableList;
}
@synthesize mainView;
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self loadingView];
}
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
[self loadingView];
}
-(void)loadingView{
Bg = [[BackGroundClass alloc]init];
tableList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableList.translatesAutoresizingMaskIntoConstraints = NO;
tableList.dataSource=self;
tableList.delegate=self;
tableList.backgroundColor = [UIColor orangeColor];
tableList.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
tableList.separatorStyle = UITableViewCellSeparatorStyleNone;
tableList.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
[tableList registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[mainView addSubview:tableList];
}
//UITableView Delegate Methods:-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] ;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"hello";
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
@end
您为表格视图设置了错误的框架。 CGRectZero
意味着表视图在mainView
上不可见。 更新下面的行
tableList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
与:-
tableList = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.