iOS7中UITableView的“静态单元格”内容无法正常工作

6
我在我的故事板中遇到了一个问题。它运行得很好,但当我尝试更改UITableView的content属性时,会导致以下错误:
-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]中的断言失败:/ SourceCache / UIKit / UIKit-2903.23 / UITableView.m 由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因是:“无法使用标识符Cell取消队列化单元格 - 必须为标识符注册一个nib或类,或连接一个故事板中的样型单元格。”
我想设计一个带有静态单元格的分组表视图。提前感谢。
代码
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 2; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return 3; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
return cell; 
} 

如果您不分享您的代码,我们将无法帮助您。 - Raheel Sadiq
  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // 返回分区数。 return 2;
}
  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 3; }
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; return cell; }
- Adnan Chaudhry
@AdnanChaudhry:将代码添加到问题中,您可以编辑问题并像这样添加代码...请查看问题下方的编辑选项。 - Lithu T.V
请查看此链接以获取更多信息。 - Rajal
7个回答

7

使用以下替代方法:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//-> 当使用原型单元格(如动态表视图)时使用此功能。

用法:

UITableViewCell *cell = [super tableView:tableView
                   cellForRowAtIndexPath:indexPath];

//-> 由于您选择了静态的TableCells,因此无需重用,而是使用您在nib中创建的单元格


太棒了,伙计! - Bryan P

7
如果您使用静态单元格,只需注释掉所有的UITableViewDatasourceDelegate方法。因此,请注释以下代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 2; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return 3; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
return cell; 
} 

希望这能帮到你!

1
非常抱歉我的回答不正确!你应该阅读这个相关的答案,这表明你可以实现UITableViewDatasourceDelegate,但不是像你的方式那样。就像这样:- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [super tableView:tableView numberOfRowsInSection:section]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return [super tableView:tableView cellForRowAtIndexPath:indexPath]; } - pingguoilove

1

我遇到了同样的问题,但我认为我找到了解决方法。

  1. 创建一个继承自UITableViewController的Controller来包装其控制器,它将具有默认的UITableViewDelegate和UITableViewDataSource方法。
  2. 删除UITableView的delegate和datasource。
  3. 从你的控制器文件中删除默认的UITableViewDelegate和UITableViewDataSource方法,因为这是静态单元格,如果存在这些方法,将不会出现。

希望能帮到你。


0
将以下代码添加到cellForRowAtIndexPath中。
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
else
{
    UIView *subview;
    while ((subview= [[[cell contentView]subviews]lastObject])!=nil)
        [subview removeFromSuperview];
}

0
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [YourArr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (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];
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

0

在普通的UITableView中,您不允许使用静态单元格。

相反,您需要使用UITableViewController来配合静态单元格。

thisthis可能会有所帮助。


0

使用Storyboard时,必须在Storyboard中注册tableview cell。这意味着必须将cell添加到表格中并设置其标识符。此标识符应在cellForRowAtIndexpath代码中使用。

编辑

对于静态单元格,每个特定的单元格都需要在nib中创建,并通过代码或nib填充内容。

阅读以下内容


我已经在Storyboard中注册了TableView的Cell,并设置了标识符。 - Adnan Chaudhry
而且单元格标识符是“Cell”吗? - Lithu T.V
当我使用带有动态原型的tableview属性内容运行项目时,它可以正常工作。但当我将此属性更改为静态单元格时,会显示错误。 - Adnan Chaudhry
静态单元格的工作方式略有不同,您需要在nib中创建单元格并加载它。 - Lithu T.V
谢谢您的时间。我是iOs的新手,您能否请解释一下? - Adnan Chaudhry
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接