Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从UITableViewCell加载多个单元格_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 如何从UITableViewCell加载多个单元格

Ios 如何从UITableViewCell加载多个单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个表,我想从xib两个单元加载。 在UITableViewCell xib文件中,我有两个单元(两个部分带有标记100和200),我希望在第一行加载第一个视图(第一个单元),在第二行加载第二个单元。 两个单元格都位于xib文件中,如下所示: 这是我的代码: #pragma mark Table View Data Source Methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { r

我有一个表,我想从xib两个单元加载。 在UITableViewCell xib文件中,我有两个单元(两个部分带有标记100和200),我希望在第一行加载第一个视图(第一个单元),在第二行加载第二个单元。 两个单元格都位于xib文件中,如下所示:

这是我的代码:

#pragma mark Table View Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    if (indexPath.section == 0) {

        CustomCell *cell = (CustomCell *)[self.table dequeueReusableCellWithIdentifier:@"myCell"];
        if (cell == nil)
        {
            NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
            NSLog(@"%@",topLevelObject);
            cell = [topLevelObject objectAtIndex:0];
            NSLog(@"%@",cell);
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.section == 1) {

        CustomCell *cell2 = (CustomCell *)[self.table dequeueReusableCellWithIdentifier:@"Cell2"];
        if (cell2 == nil)
        {
            NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];

            for (id currentObject in topLevelObject) {
                if ([currentObject isKindOfClass:[CustomCell class]]) {
                    cell2 = (CustomCell *)currentObject;
                    break;
                }
            }
        }
        cell2.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell2;
    }
    return nil;        
}

我不知道我的错误在哪里,因为当运行代码时,我只看到tableView中的第一个单元格

您使用的是来自XIB的第一个单元格,位于section=0 单元格=[topLevelObject对象索引:0]; 所以,用同样的方法,你可以得到第1节索引1处的第二个单元格
单元格=[topLevelObject对象索引:1]

用于显示两个不同的tableview单元格。您需要创建两个不同的tableview单元格子列表。