Ios 如何在特定的indexath.row上显示自定义tableviewcell

Ios 如何在特定的indexath.row上显示自定义tableviewcell,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我创建了tableview和两个不同的自定义单元格。 假设cell1和cell2 现在我想在tableview中添加单元格,如下模式所示 第1单元 第1单元 第1单元 细胞2 第1单元 第1单元 第1单元 细胞2 以此类推,我有在cell1中显示的数据计数,但我不知道cell2是否计数,因为它应该是动态计数,我的意思是每3个cell1数据之后,第4个单元格将是cell2 这是我的tableview代码 #pragma mark - TableView Delegate Methods - (NS

我创建了tableview和两个不同的自定义单元格。 假设cell1和cell2

现在我想在tableview中添加单元格,如下模式所示

第1单元

第1单元

第1单元

细胞2

第1单元

第1单元

第1单元

细胞2

以此类推,我有在cell1中显示的数据计数,但我不知道cell2是否计数,因为它应该是动态计数,我的意思是每3个cell1数据之后,第4个单元格将是cell2

这是我的tableview代码

#pragma mark - TableView Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{
return [arrayRecipeCategoryNAME count];
//return 22;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:   (NSIndexPath *)indexPath{
return 175;
}

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

if((indexPath.row % 3) == 0){
    HomeTableViewCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
    return cell2;
}
else{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.lblRecipeCategory.text = [arrayRecipeCategoryNAME objectAtIndex:indexPath.row];
    cell.lblRecipeCategory.font = FONT_COMFORTAA_BOLD(28);
    cell.imgRecipeCategory.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrayRecipeCategoryImage objectAtIndex:indexPath.row]]];
    return cell;
}
}

你可以这样使用它

var Index = [indexpath.Row] + 1

 if Index%4 == 0
 {
    // return cell 2
 }
 else{
    // return cell 1
 }

如果它是固定的,每四个单元格将是cell2,然后尝试使用以下代码,这可能会有所帮助

    Int *row = indexPath.row + 1
    if(row % 4 == 0){
        HomeTableViewCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
        return cell2;
    }
    else{
        HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.lblRecipeCategory.text = [arrayRecipeCategoryNAME objectAtIndex:indexPath.row];
        cell.lblRecipeCategory.font = FONT_COMFORTAA_BOLD(28);
        cell.imgRecipeCategory.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrayRecipeCategoryImage objectAtIndex:indexPath.row]]];
        return cell;
    }

首先,必须调整
numberOfRowsInSection:
中返回的行数,以显示额外的单元格。
然后在
cellforrowatinexpath:
中,您需要调整数组索引以匹配用于数据源数组的正确序列
ArrayReciecCategoryName
ArrayReciecCategoryImage

以下内容适用于您当前的设计:

#define INTERVAL 4

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
    NSInteger count = arrayRecipeCategoryNAME.count;

    NSInteger numberOfUnmappedRows = count / INTERVAL;
    NSInteger totalNumberOfRows = (count + numberOfUnmappedRows);

    return totalNumberOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(((indexPath.row + 1) % INTERVAL) == 0) {
        HomeTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
        return cell;
    }
    else {
        NSInteger numberOfUnmappedCellsBeforeCurrent = indexPath.row / INTERVAL;

        //Use following as the index for your datasource arrays
        NSInteger translatedIndex = indexPath.row - numberOfUnmappedCellsBeforeCurrent;

        HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

        cell.lblRecipeCategory.text = arrayRecipeCategoryNAME[translatedIndex];
        //...
        cell.imgRecipeCategory.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", arrayRecipeCategoryImage[translatedIndex]]];

        return cell;
    }
}

可能是通过数据检测,而不是通过行检测。例如:arrayRecipeCategoryNAME[indexPath.row].cell2==“一些数据”,然后返回HomeTableViewCell2。相反,这样做的好处是有一天服务器端会更改逻辑。你赢了。首先,如果(indexath.row+1)%3==0,那么它应该是
,因为
0%N
总是0。但问题是您的
arrayRecipateGoryName
仅适用于
cell1
。你需要尝试一种不同的方法,你可以这样做<代码>如果((indexPath.row+1)%4==0{//返回单元格2}否则{//返回单元格1}
@staticVoidMan你说得对,我不知道cell2的计数,因为它完全取决于cell1的计数可以被3整除,这就是我被卡住的地方now@TejasPatel
cell2
什么时候出现?为什么?它是链接到自己的数据源还是从
ArrayReciecCategoryName
中获取?它像一个标题吗?这是我就像在上面的cell1 cell1 cell1 cell1 Cell2上的cell1它与cell1的数据重叠