如何在ios的表视图中显示不同的自定义单元格

时间:2013-12-27 05:53:16

标签: ios objective-c uitableview

1。在下面给出的编写代码示例中我的方法 -

        (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
        static NSString *simpleTableIdentifier = @"SimpleTableCell";
        static NSString *simpleTableIdentifier2 = @"SimpleTableCell2";
        if (indexPath.row== 0) {


        SimpleTableCell *cell = nil;
        cell=(SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        }

        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
           cell = [nib objectAtIndex:0];
        }
        if (indexPath.row == 1) {
            SimpleTableCell2 *cell2 = nil;
            cell2=(SimpleTableCell2 *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
        }
        if (cell2 == nil)
        {
            NSArray *nib2 = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell2" owner:self options:nil];
           cell2 = [nib2 objectAtIndex:0];
        }


        cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
        cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
        cell.prepTimeLabel.text = @"detail";// [prepTime objectAtIndex:indexPath.row];
        //cell.imageView.image = [UIImage imageNamed:@"creme_brelee.jpg"];
        cell2.nameLabel.text = [tableData objectAtIndex:indexPath.row];

        return cell;
    }

可以任何人帮助我,我写的正确代码becoz它会给出一个错误     错误是使用不明确的标识符单元格和cell2     我不明白这是什么错误     在cell2之前制作cell2之后它会给出错误它完美地工作     帮我     感谢

6 个答案:

答案 0 :(得分:1)

它给出了错误,因为你已经声明了这样:

if (indexPath.row== 0) {
    SimpleTableCell *cell = nil;
    cell=(SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    }

并且您正在使用此条件的本地范围之外的单元格变量。 这样做:

SimpleTableCell *cell = nil; if (indexPath.row== 0) { cell=(SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; }

还要确保已导入标题SimpleTableCell.h。

希望有所帮助!

答案 1 :(得分:0)

 if (indexPath.row== 0) {


        SimpleTableCell *cell = nil;
        cell=(SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

     if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
           cell = [nib objectAtIndex:0];
        }
return cell;    
}




     if (indexPath.row == 1) {
            SimpleTableCell2 *cell2 = nil;
            cell2=(SimpleTableCell2 *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];

    if (cell2 == nil)
        {
            NSArray *nib2 = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell2" owner:self options:nil];
           cell2 = [nib2 objectAtIndex:0];
        }
return cell2;        
}

 UITableViewCell *commonCell = [tableView dequeueReusableCellWithIdentifier:@"commonToALL"];
    if (commonCell == nil) {
        commonCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"commonToALL"];


    }
return commonCell;

试试这个。这将为索引0和1创建两个特定单元格,而为剩余部分创建两个常规单元格,因为您有4行。

答案 2 :(得分:0)

if (indexPath.row == 1) {
   SimpleTableCell2 *cell2 = nil;
   cell2=(SimpleTableCell2 *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
   //This is ok
   cell2.nameLabel.text = [tableData objectAtIndex:indexPath.row]; 
}
//No you cant access outside
cell2.nameLabel.text = [tableData objectAtIndex:indexPath.row]; 

在这里,您已在if块中声明cell2。因此,您只能在此范围内使用此变量。当您尝试访问外部的cell2时,您将使用未声明的错误。

答案 3 :(得分:0)

未声明的标识符表示您未注册单元类。 您应该在 viewDidLoad

中执行此操作
-(void)viewDidLoad
{
    [self.tableView registerClass:[SimpleTableCell class] forCellReuseIdentifier:@"SimpleTableCell"];
    [self.tableView registerClass:[SimpleTableCell2 class] forCellReuseIdentifier:@"SimpleTableCell2"];
}

但是,处理单元格标识符时, NSStringFromClass 方法更好:

-(void)viewDidLoad
{
    [self.tableView registerClass:NSStringFromClass([SimpleTableCell class]) forCellReuseIdentifier:@"SimpleTableCell"];
    [self.tableView registerClass:NSStringFromClass([SimpleTableCell2 class]) forCellReuseIdentifier:@"SimpleTableCell2"];
}

如果您没有注册一个单元格类, dequeueReusableCellWithIdentifier 可以返回 nil ,如文档中所指定的那样:

This method dequeues an existing cell if one is available or creates a new one
using the class or nib file you previously registered. If no cell is available
for reuse and you did not register a class or nib file, this method returns nil.

附加说明:保持视图控制器方法尽可能轻薄。

答案 4 :(得分:0)

你可以试试这些代码:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath  (NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
static NSString *simpleTableIdentifier2 = @"SimpleTableCell2";
if (indexPath.row== 0) {
    SimpleTableCell *cell = nil;
    cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil){
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    cell.prepTimeLabel.text = @"detail";// [prepTime objectAtIndex:indexPath.row];
    //cell.imageView.image = [UIImage imageNamed:@"creme_brelee.jpg"];

    return cell;
}

SimpleTableCell2 *cell2 = nil;
if (indexPath.row == 1) {
    cell2 = (SimpleTableCell2 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
    if (cell2 == nil){
        NSArray *nib2 = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell2" owner:self options:nil];
        cell2 = [nib2 objectAtIndex:0];
    }

    cell2.nameLabel.text = [tableData objectAtIndex:indexPath.row];
}

return cell2;

}

答案 5 :(得分:0)

您最好在SimpleTableCell.xib中检查自定义单元格的标记 enter image description here

标签是否为0

只有一个自定义单元格可以像:

    (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *simpleTableIdentifier = @"SimpleTableCell";
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
       cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    cell.prepTimeLabel.text = @"detail";// [prepTime objectAtIndex:indexPath.row];
    //cell.imageView.image = [UIImage imageNamed:@"creme_brelee.jpg"];
    if (indexPath.row == 1) {
        [cell.thumbnailImageView.image setHidden:Yes];
        [cell.prepTimeLabel.text setHidden:Yes];
    }
    return cell;
}