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;
    }
}
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]);

返回空

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

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

4

3 回答 3

3

您没有在 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;

希望这可以帮助

于 2013-04-05T09:16:22.503 回答
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;
    }
}
于 2013-04-05T09:18:08.617 回答
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];

}

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


}

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

于 2013-04-05T10:18:18.177 回答