developerWab 2013-07-19 02:26 采纳率: 0%
浏览 2348
已采纳

UITableViewCell重复UIImages

资源包有10张图片:

sample_pic1.jpg
sample_pic2.jpg
sample_pic3.jpg
...
sample_pic10.jpg

有一个100行的tableView,我想要每10行一个的格式显示上面那10张图片。基本实现了每十行一重复,但是我想要一个重复图片的逻辑方式。

代码:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"Inside cellForRowAtIndexPath ... indexPath.row: %d", indexPath.row);

    static NSString *CellIdentifier = @"Cell";
    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // If no cell is available, create a new one using the given identifier.
    if (cell == nil)
    {
        // Use the default cell style.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }//nil-check
        int imageCounter = (indexPath.row+1);
        //ofcourse this logic doesn't work for rows 21 and up!
        if (imageCounter > 10)
        {
            imageCounter = (indexPath.row+1) - 10;
        }
        NSLog(@"indexPath.row: %d ... imageCounter: %d ...", indexPath.row, imageCounter);

        NSMutableString *tmpImgStr = [NSMutableString stringWithFormat:@"sample_pic%d.jpg", imageCounter];

        UIImage *myimage = [UIImage imageNamed:tmpImgStr];
        cell.imageView.image = myimage;
}

这里indexPath.row从0到99。

  • 写回答

1条回答 默认 最新

  • redCoral_ 2013-07-19 05:47
    关注

    使用模运算符实现。考虑到从1开始,图片编号也应该从1开始:

    int imageCounter = (indexPath.row % 10) + 1;
    NSMutableString *tmpImgStr = [NSMutableString stringWithFormat:@"sample_pic%d.jpg", imageCounter];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题