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

时间:2013-04-05 09:14:20

标签: ios ipad uitableview

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

返回null

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

  

由于未捕获的异常而终止应用   'NSInternalInconsistencyException',原因:'UITableView dataSource   必须从tableView:cellForRowAtIndexPath:'

返回一个单元格

3 个答案:

答案 0 :(得分:3)

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

出行的方式是:

  1. 询问具有CellIdentifier

  2. 的可重复使用单元格的表格视图
  3. 如果单元格为零,则使用CellIdentifier分配新单元格,以便将来可以重复使用。

  4. 所以你可以这样:

    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;
    

    希望这会有所帮助

答案 1 :(得分: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;
    }
}

答案 2 :(得分: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文件