开发者社区> 问答> 正文

关于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。

展开
收起
爵霸 2016-03-17 15:02:19 2051 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载