【问题标题】:i want to create CustomCell But it give some error, How to Solve it?? error is describe below我想创建 CustomCell 但它给出了一些错误,如何解决它?错误描述如下
【发布时间】:2013-04-05 09:14:20
【问题描述】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}
cell.cellDate.text=@"date";
cell.cellDescription.text =@"Description";
cell.cellImageview.image = [UIImage imageNamed:@"sample.png"];
cell.cellTitle.text = @"Title";
NSLog([cell Description]);
return cell;

}

 NSLog([cell Description]);

返回空值

以上是我的方法它给出以下错误: 这里

由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'UITableView 数据源 必须从 tableView 返回一个单元格:cellForRowAtIndexPath:'

【问题讨论】:

  • 在内部分配您的自定义单元格 if (cell == nil) {}
  • @vitality 向您展示的内容是正确的。底线是您需要初始化一个新单元格。所以在(cell==nil)之后,只需插入这一行:cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

标签: ios ipad uitableview


【解决方案1】:

您没有在 if (cell == nil) 位内分配新单元格。您需要分配一个新的UITableViewCell 才能返回。

出队的工作方式是:

  1. 使用CellIdentifier向表格视图询问可重复使用的单元格

  2. 如果单元格为 nil,则使用您的 CellIdentifier 分配一个新单元格,以便将来重复使用。

所以你可以这样做:

static NSString *CellIdentifier = @"CustomCell";
//Ask for a cell with the cell identifier
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//If the cell is nil
if (cell == nil) 
{
//Allocate a new cell with the identifier
    cell = [[CustomCell alloc] init];//put your actual init method here
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
{
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.cellDate.text=@"date";
cell.cellDescription.text =@"Description";
cell.cellImageview.image = [UIImage imageNamed:@"sample.png"];
cell.cellTitle.text = @"Title";
NSLog([cell Description]);
return cell;

希望对你有帮助

【讨论】:

  • 在实现新代码后出现此错误:体系结构 i386 的未定义符号:“_OBJC_CLASS_$_CustomCell”,引用自:MasterViewController.old 中的 objc-class-ref:未找到体系结构的符号i386 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
  • 执行此操作无法将 CustomCell.xib 加载到 tableview 单元格中你知道吗
【解决方案2】:

改变这个:

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}

收件人:

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];

if (cell == nil) 
{
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
          cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
}

【讨论】:

  • 您确实显示了完全相同的内容,只是您为 cell==nil 添加了一个额外的检查,它总是为零
  • CustomCell *cell = nil;使每次在表格视图中创建一个新单元格,它不会重复使用单元格..
  • 这将始终返回 nil - 您有时需要使用重用标识符分配单元格。 tableview 收集这些并在可能的情况下重新使用它们,但如果它需要一个新的单元格,那么你需要分配它。
  • 又是如何初始化的呢?永远不会创建新单元格。
  • 您不需要更改CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 行。这是对的。您需要做的是创建一个新单元格,如果该单元格为零,即一个单元格不可重复使用...
【解决方案3】:
enter code here// Customize the appearance of table view cell
   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"CustomCell";
   CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:  CellIdentifier];

if (cell == nil)
{
     cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}

cell.cellDate.text=@"date";
cell.cellDescription.text =@"Description";
cell.cellImageview.image = [UIImage imageNamed:@"sample.png"];
cell.cellTitle.text = @"Title";
return cell;


}

不要忘记在编译源选项卡中添加customcell的.m文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 2020-07-21
    • 2010-12-20
    • 2017-11-12
    • 2021-05-09
    • 2018-03-14
    相关资源
    最近更新 更多