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 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问
  • ¥15 初学者如何快速上手学习stm32?
  • ¥15 如何自动更换布娃娃图片上的衣服
  • ¥15 心理学eprime编程