【问题标题】:How to display custom tableviewcell on specific indexpath.row如何在特定 indexpath.row 上显示自定义 tableviewcell
【发布时间】:2018-03-30 06:28:05
【问题描述】:

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

现在我想像下面的模式在表格视图中添加单元格

单元格1

单元格1

单元格1

单元格2

单元格1

单元格1

单元格1

单元格2

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

这是我的表格视图代码

#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;
}
}

【问题讨论】:

  • 可能通过数据而不是行来检测。例如:arrayRecipeCategoryNAME[indexPath.row].cell2 == "some data",然后返回 HomeTableViewCell2 否则相反,这样做的好处是有一天服务器端改变了逻辑.你赢了。
  • 首先它应该是if (indexPath.row + 1) % 3 == 0,因为0 % N 总是0。但问题是你的arrayRecipeCategoryNAME,它只适用于cell1。你需要尝试不同的方法
  • 你可以这样做......if ((indexPath.row+1) % 4 == 0 { // return cell 2 } else{ // return cell 1 }
  • @staticVoidMan 是的,我不知道 cell2 的计数,因为它完全取决于能被 3 整除的 cell1 计数,这就是我现在卡住的地方
  • @TejasPatel cell2 什么时候出现?为什么?它是链接到自己的数据源还是从arrayRecipeCategoryNAME 获取?它像标题吗?

标签: ios objective-c uitableview


【解决方案1】:

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

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

#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;
    }
}

【讨论】:

    【解决方案2】:

    你可以这样使用它。

    var Index = [indexpath.Row] + 1
    
     if Index%4 == 0
     {
        // return cell 2
     }
     else{
        // return cell 1
     }
    

    【讨论】:

    • 这让我像跟随 cell1 cell1 cell1 cell1 on top cell2
    • 其重叠cell1的数据
    【解决方案3】:

    如果固定每四个单元格为 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;
        }
    

    【讨论】:

      猜你喜欢
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 2014-08-08
      相关资源
      最近更新 更多