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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?