2013-07-18 49 views
1

這再次是一個數學問題比任何其他更多。我在我的資源包中有10張圖片UITableViewCell重複UIImages

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; 

} 

回答

6

使用模運算符重複圖像數量每10行,並添加1〜考慮到你的圖像編號從1開始:

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

1/2方式輸入相同的東西。乾淨,簡單,真棒。 – feliun

+0

我的天哪。這可能不是那麼簡單!謝謝,它有效。 –