Ann*_*565 19 objective-c uitableview ios
我有一个简单的问题,但我不明白是什么.我有一个UIViewControler(被叫RootController)加载一个UIView(被叫SecondView)包含一个tableView.问题是UIView调用numberOfSectionsInTableView和numberOfRowsInSection不调用,cellForRowAtIndexPath并且不显示表视图.代码RootViewController是:
SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:secondView];
Run Code Online (Sandbox Code Playgroud)
而代码SecondView是:
@interface SecondView () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,retain) UITableView *table;
@end
@implementation SecondView
@synthesize table;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.table = [[UITableView alloc] init];
self.table.delegate = self;
self.table.dataSource = self;
[self addSubview:self.table];
}
return self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"Prova";
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
Run Code Online (Sandbox Code Playgroud)
你能帮我找到问题吗?谢谢.
spf*_*ich 10
如果在不同的线程上调用reloadData,也可能发生这种情况.确保它在主线程上运行,因为所有UI内容都必须在主线程上发生.
dispatch_async(dispatch_get_main_queue(),{
self.myTableView.reloadData()
});
Run Code Online (Sandbox Code Playgroud)
我的问题是我有一个简单的类来实现我的委托和数据源,但是简单类的生命周期太短了.
我在做
MyDataSourceClass* myClass = [[MyDataSourceClass alloc] initWithNSArray:someArray];
tableView.dataSource = self.tableViewDataSource;
tableView.delegate = self.tableViewDataSource;
[tableView reloadData];
// end of function, myClass goes out of scope, and apparently tableView has a weak reference to it
Run Code Online (Sandbox Code Playgroud)
需要做
self.tableDataSource = [[MyDataSourceClass alloc] initWithNSArray:someArray];
tableView.dataSource = myClass;
tableView.delegate = myClass;
[tableView reloadData];
// now at the end of the function, tableDataSource is still alive, and the tableView will be able to query it.
Run Code Online (Sandbox Code Playgroud)
请注意,上面的代码是来自内存的伪代码.从中获取"确保您的数据源/委托长寿"的概念,但不要复制粘贴它,因为您还需要做其他事情(比如设置框架等).
在XCode6中,当在Storyboard上的tableView上应用"添加缺失约束"来调整视图时,我发生了同样奇怪的问题.
要在Storyboard上解决此问题,请首先清除约束:

然后以下列方式应用约束:

| 归档时间: |
|
| 查看次数: |
19964 次 |
| 最近记录: |