Ios 如何在TableView中添加多个CustomCell(在我的示例中为3)

Ios 如何在TableView中添加多个CustomCell(在我的示例中为3),ios,uitableview,ios7,Ios,Uitableview,Ios7,我正在创建一个演示应用程序,其中我需要在tableView中设置3个自定义单元格。我可以在所有三行中添加第一个customCell,但当我添加3个单元格时,应用程序崩溃 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UI

我正在创建一个演示应用程序,其中我需要在tableView中设置3个自定义单元格。我可以在所有三行中添加第一个customCell,但当我添加3个单元格时,应用程序崩溃

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 1;
    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Create Rows in a tableView
        return 3;
    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        static NSString *cellIdentifier = @"customCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            // First CustomCell
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FirstCustomCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            if (indexPath.row ==1) {
                // Second CustomCell
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
                cell = [nib objectAtIndex:0];

            }
            if (indexPath.row == 2) {
                // Third CustomCell
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PhoneTableViewCell" owner:self options:nil];
                cell = [nib objectAtIndex:1];

            }
        }

        return cell;
    }
当我运行应用程序时,它会崩溃,这是错误消息:
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[\uu NSArrayM objectAtIndex:]:索引1超出边界[0..0]”

问题就在这里

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PhoneTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:1];
                         ^^^
应该是

cell = [nib objectAtIndex:0];

问题是cell=[nib objectAtIndex:1]必须始终单元格=[nib objectAtIndex:0]因为只有一个该名称的Nib

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *cellIdentifier = @"customCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        // First CustomCell
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FirstCustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        if (indexPath.row ==1) {
            // Second CustomCell
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];

        }
        if (indexPath.row == 2) {
            // Third CustomCell
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PhoneTableViewCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];

        }
    }

    return cell;
}

为什么要使用cell=[nib objectAtIndex:1];当indexPath.row==2时?
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    if (Your_Condition==1) {
        DoctorListCell *cell = (DoctorListCell *)[tableView dequeueReusableCellWithIdentifier:@"DoctorListCell"];
        if(cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"iPhone_DoctorListCell"
                                                         owner:self
                                                       options:nil];
            cell = (DoctorListCell *)[nib objectAtIndex:0];
        }
        return cell;

    }
    else if(your_Condition==2)
    {

        ArticleListCell *cell = (ArticleListCell *)[tableView dequeueReusableCellWithIdentifier:@"ArticleListCell"];
        if(cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"iPhone_ArticleList"
                                                         owner:self
                                                       options:nil];
            cell = (ArticleListCell *)[nib objectAtIndex:0];
        }
        return cell;
    }
    else if(your_condition==3)
    {

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

        return cell;
    }
    return nil;
}