【问题标题】:UITableView Content "Static cell" not working in iOS7UITableView 内容“静态单元格”在 iOS7 中不起作用
【发布时间】:2013-11-04 07:01:33
【问题描述】:

我的故事板有问题。它工作正常,但是当我尝试更改 UITableView 的内容属性时,它导致以下错误

-[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; 
} 

【问题讨论】:

  • 如果您不分享您的代码,我们将无法为您提供帮助
  • - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // 返回节数。返回 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];返回单元格; }
  • @AdnanChaudhry :将代码添加到问题中,您可以编辑问题并添加这样的代码..查看问题下的编辑选项
  • 更多信息请见link

标签: iphone ios7 xcode5


【解决方案1】:

如果你使用静态单元格,只需注释所有 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; 
} 

希望这对您有所帮助!

【讨论】:

  • 很抱歉我的回答不正确!你应该阅读This related answer,这表明你可以实现 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]; }
【解决方案2】:

而不是使用:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//-> 这在使用原型单元格时使用(例如)Dynamic TableView

用途:

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

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

【讨论】:

  • 朋友的回答很好!
【解决方案3】:

我遇到了同样的问题,我想我找到了解决这个问题的方法。

  1. 创建一个从 UITableViewController 扩展来包装其控制器的控制器,它将具有默认的 UITableViewDelegate 和 UITableViewDataSource 方法。
  2. 移除 UITableView 委托和数据源
  3. 从你的控制器文件中删除默认的 UITableViewDelegate 和 UITableViewDataSource 方法,因为它是静态单元格,所以如果它们存在,就不会出现

希望对你有帮助。

【讨论】:

    【解决方案4】:

    使用storyboard时,tableview单元格必须在storyboard中注册,即必须将单元格添加到表格中,并且必须设置其标识符。这个标识符应该在cellForRowAtIndexpath的代码中使用

    编辑

    如果是静态单元格,则需要在 nib 中创建每个特殊单元格并通过代码或 nib 填充内容

    阅读本文

    【讨论】:

    • 我已将 tableview 单元格注册到情节提要中并设置了标识符。
    • 并且单元格标识符是“Cell”?
    • 当我使用带有动态原型的 tableview 属性内容运行项目时,它工作正常。但是当我将此属性更改为静态单元格时,会显示错误。
    • 感谢您的时间和支持。是的,单元格标识符是“单元格”。
    • 静态单元格的工作方式略有不同,您需要在 nib 中创建单元格并加载它
    【解决方案5】:

    您不能在普通 UITableView 中使用静态单元格。

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

    thisthis 可能会或可能会有所帮助。

    【讨论】:

      【解决方案6】:
      - (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;
      }
      

      【讨论】:

        【解决方案7】:

        将此代码添加到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];
        }
        

        【讨论】:

          猜你喜欢
          • 2015-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-29
          • 2020-04-03
          • 1970-01-01
          相关资源
          最近更新 更多