[英]iOS - Observer to observe tableView dequeue Reusable Cell
[英]iOS 7 TableView dequeue not working
我有一個非常煩人的問題,我不確定為什么會這樣。
我已經設置了“表格”視圖,在單元格中具有正確的標識符,但是它仍然給我“無法使具有標識符單元格的單元出隊-必須為該標識符注冊一個筆尖或一個類,或者在情節提要中連接原型單元格”當我運行它時。
代碼看起來像這樣
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:@"name"];
return cell;
}
任何幫助都將是很大的,因為我想撕開頭發。 該表已連接到該類,然后Cell具有該@“ Cell”標識符
重新檢查所有4個步驟
步驟1:您的cellForRowAtIndexPath應該看起來像這樣
- (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:nil];
}
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:@"name"];
return cell;
}
步驟2:您的“ ViewController.m”文件應如下所示
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
步驟3:請檢查-單擊視圖控制器,移至連接檢查器-查看此檢查器中是否還有其他不需要的連接。
步驟4: Tableview單元屬性檢查器應為[]
代替:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
嘗試:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果那之后不起作用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
加:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
正確,您需要注冊一個單元以供重用。 創建表視圖后,可以通過編程方式完成此操作:
static NSString *cellIdentifier = @"Cell";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
或者,也可以在Interface Builder中通過選擇單元格並導航到屬性檢查器並更改單元格的ID來完成此操作。
您不檢查是否分配了單元格。您可以嘗試以下代碼。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
// add a placeholder cell while waiting on table data
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
}
在文檔大綱,驗證您的原型UITableViewCell
是一個孩子UITableView
。
在“屬性檢查器”中,驗證是否在情節提要中正確設置了“單元格標識符”和“單元格樣式”:
在身份檢查器中,驗證是否已將類設置為UITableViewCell
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:@"name"];
}
return cell;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.