【发布时间】:2013-11-18 15:00:11
【问题描述】:
我为 UITableView 创建了 .h 和 .m 文件,分别称为 mainTableViewgm.h 和 mainTableViewgm.m。我正在调用 -initWithFrame: 方法从我的主视图控制器到这个 mainTableViewgm.m 实现文件
[[mainTableViewgm alloc]initWithFrame:tableViewOne.frame]
请注意,此表视图位于我的主视图控制器中。但是我已经为 tableView 创建了单独的文件,并且还在 storyboard 中将自定义类设置为 mainTableViewgm。
-initWithFrame: 方法如下所示
- (id)initWithFrame:(CGRect)frame
{
//NSLog(@"kource data");
self = [super initWithFrame:frame];
if (self)
{
[self setDelegate:self];
[self setDataSource:self];
[self tableView:self cellForRowAtIndexPath:0];
[self tableView:self numberOfRowsInSection:1];
// Initialization code
}
return self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"kource data");
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"kource data2");
UITableViewCell*cellOne =[[UITableViewCell alloc]init];
cellOne.detailTextLabel.text=@"text did appear";
return cellOne;
}
-initWithFrame: 与此方法中的“if (self)”块一起被很好地调用。但问题是 numberOfRowsInSection: 和 cellForRowAtIndexPath: 不会在这里被自动调用。 kource data/kource data2 永远不会出现在日志中。我该怎么做才能加载表格? delegate/datasource 是否设置错误?
我必须提到我还设置了UITableViewDelegate 和UITableviewDataSource 协议:
@interface mainTableViewgm : UITableView <UITableViewDelegate,UITableViewDataSource>
@end
我们将不胜感激。谢谢。
【问题讨论】:
-
把cellforrow的减速和节数从initwithframe方法中取出再试一下。
标签: ios iphone objective-c uitableview