2013-04-05 46 views
1
- (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; 
    } 
} 
[email protected]"date"; 
cell.cellDescription.text [email protected]"Description"; 
cell.cellImageview.image = [UIImage imageNamed:@"sample.png"]; 
cell.cellTitle.text = @"Title"; 
NSLog([cell Description]); 
return cell; 

}我想创建CustomCell但它给一些错误,如何解决它?错误描述如下

NSLog([cell Description]); 

返回null

以上就是我的方法它提供了以下错误: 这里

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

+0

分配您的自定义单元格里面如果(细胞==零){} – NANNAV 2013-04-05 09:24:26

+0

什么@vitality正显示出你是正确的。底线是你需要初始化一个新的细胞。因此,在'(cell == nil)'之后,只需插入以下这行:'cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];' – Lefteris 2013-04-05 09:25:22

回答

3

您还没有分配内部的新小区的如果(cell == nil)位。您需要分配一个新的UITableViewCell才能返回。

去排队的工作方式是:

  1. 问表视图可重复使用的电池与CellIdentifier

  2. 如果电池是零,然后使用您的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; 
} 
[email protected]"date"; 
cell.cellDescription.text [email protected]"Description"; 
cell.cellImageview.image = [UIImage imageNamed:@"sample.png"]; 
cell.cellTitle.text = @"Title"; 
NSLog([cell Description]); 
return cell; 

希望这有助于

+0

geting这个错误代码实现了新代码:架构i386的未定义符号: “_OBJC_CLASS _ $ _ CustomCell”引用来自: MasterViewController.o中的objc-class-ref 未找到架构i386的ld:symbol(s) clang:错误:linker命令失败,退出代码1(使用-v查看调用) – 2013-04-05 10:02:34

+0

perfoming这不能将CustomCell.xib加载到tableview单元中你有什么想法 – 2013-04-05 10:52:41

1

更改此:

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; 
    } 
} 
+1

你确实显示了完全相同的东西,只是你添加了一个额外的检查'cell == nil',这总是会变成nill – Lefteris 2013-04-05 09:21:06

+0

CustomCell * cell = nil;使每次在表视图中创建一个新的单元格,它不会重用单元格.. – NANNAV 2013-04-05 09:22:55

+0

这将始终返回零 - 你在某些时候需要分配与重用标识符的单元格。如果可能的话,tableview收集这些并重新使用它们,但如果它需要一个新的单元格,那么你需要分配它。 – 2013-04-05 09:23:02

0
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]; 

} 

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


} 

不要忘记添加customcell的.m文件在编译源标签

相关问题