UITableViewCell重复UIImages

时间:2013-07-18 15:52:08

标签: iphone ios uitableview

这再次是一个数学问题而不是其他任何问题。我的资源包中有10张图片

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

我有一个包含100行的表格视图。我想要做的是在细胞图像中每10行显示10个样本图片。基本上他们每10行重复一次。我遇到的问题是我想不出重复图像的逻辑?

这就是我所拥有的。

注意:这里indexPath.row从0到99继续。所以我可以使用

// 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;

}

1 个答案:

答案 0 :(得分:6)

使用模运算符每10行重复一次图像编号,并添加1以考虑您的图像从1开始编号:

int imageCounter = (indexPath.row % 10) + 1;
NSMutableString *tmpImgStr = [NSMutableString stringWithFormat:@"sample_pic%d.jpg", imageCounter];