![](/img/trans.png)
[英]cellForRowAtIndexPath never gets called, but numberOfRowsInSection is
[英]cellForRowAtIndexPath not called but numberOfRowsInSection called
我有一個簡單的問題,但我不明白是什么。 我有一個UIViewControler
(稱為RootController
),它加載一個包含tableView
的UIView
(稱為SecondView
)。 問題是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];
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;
}
你能幫我找出問題嗎? 謝謝你。
需要設置UITableView
的Frame
如果在不同的線程上調用 reloadData,也可能發生這種情況。 確保它在主線程上運行,因為所有 UI 內容都必須在主線程上發生。
dispatch_async(dispatch_get_main_queue(),{
self.myTableView.reloadData()
});
我的問題是我有一個簡單的類來實現我的委托和數據源,但是這個簡單類的生命周期太短了。
我在做
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
需要做的
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.
請注意,上面的代碼是來自內存的偽代碼。 從中獲取“確保您的數據源/委托壽命長”的概念,但不要復制粘貼它,因為您還需要做其他事情(例如設置框架等)。
在 XCode6 中,當在 Storyboard 上的 tableView 上應用“添加缺少的約束”以調整視圖時,我遇到了同樣的奇怪問題。
要解決 Storyboard 上的這個問題,首先要明確約束:
然后以下列方式應用約束:
您只能在調用 viewDidLoad之后調用視圖控制器的視圖。 您無法在 init 方法中與 self.view 交互
- (void)viewDidLoad {
[super viewDidLoad];
self.table = [[UITableView alloc] initWithFrame:self.view.bounds];
self.table.delegate = self;
self.table.dataSource = self;
[self addSubview:self.table];
}
在您的情況下,您需要使用框架初始化您的 tableview(如上面代碼中的建議)。 只需確保在 viewController 的 viewDidLoad 中添加代碼
SecondView *secondView = [[seconddView alloc] initWithFrame:CGRectMake(0, 60, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview:secondView];
我有和你一樣的問題:
我只有一種來自委托協議的方法調用,這是numberOfRowsInSection 。 同時我有第二種方法cellForRowAtIndexPath沒有被調用。
這種行為的原因是我的numberOfRowsInSection返回了 0 行並且cellForRowAtIndexPath沒有調用,因為它需要繪制 0 個單元格。
我會評論 Hussain Shabbir 的回答,以澄清它,但由於我還不能發表評論,我會發布一個答案。
我有完全相同的問題。 numberOfRowsInSection
會觸發,但cellForRowAt
不會。 我撕開我的代碼尋找原因,但原因不在代碼中。
解決方案(對我來說)在故事板中。 我沒有為表視圖設置約束。 選擇表格視圖,單擊“添加新約束”圖標(看起來像一個方形 TIE 戰斗機)並為頂部、底部、前導和尾隨設置約束。 然后將調用cellForRowAt
並填充您的表格。
我希望這可以幫助別人。
此解決方案特定於我的情況,但可能對某人有所幫助。
我有同樣的問題。 我使用的是計算屬性而不是存儲屬性; 所以每次我調用 tableView 時,我都會得到一個新的。
我有這個代碼:
var tableView: UITableView{
let tableView = UITableView()
tableView.dataSource = self
tableView.delegate = self
tableView.register(SomeCell.self, forCellReuseIdentifier: cellID)
return tableView
}
這是固定代碼:
lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.dataSource = self
tableView.delegate = self
tableView.register(SomeCell.self, forCellReuseIdentifier: cellID)
return tableView
}()
刪除 UITableView 並在您的 ViewController 中再次添加
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.