1

这又是一个数学问题,而不是其他任何问题。我的资源包中有 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;

}
4

1 回答 1

6

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

int imageCounter = (indexPath.row % 10) + 1;
NSMutableString *tmpImgStr = [NSMutableString stringWithFormat:@"sample_pic%d.jpg", imageCounter];
于 2013-07-18T15:57:18.607 回答