2013-07-18 3 views
1

이것은 다시 다른 것보다 수학 문제입니다. 내 리소스 번들에 10 장의 사진이 있습니다.UITableViewCell 반복 UIImages

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

나는 100 개의 행이있는 테이블 뷰가 있습니다. 셀 이미지에 10 개의 샘플 사진을 10 줄씩 보여주고 싶습니다. 기본적으로 그들은 10 줄마다 계속 반복합니다. 내가 가진 문제는 이미지를 반복하는 논리를 생각할 수 없다는 것입니다.

이것은 내가 가지고있는 것입니다.

참고 : 여기 indexPath.row가 99 0에서가는 계속 그래서 그게 내가 고려하는 이미지 번호 각 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; 

} 

답변

6

사용 모듈로 연산자를 사용하고, 하나를 추가 할 수있는 일입니다 귀하의 이미지는 번호가 매겨져 시작 1 :

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

같은 방식으로 입력합니다. 깨끗하고, 간단하고, 굉장합니다. – feliun

+0

맙소사. 이것은 아마도 그렇게 간단하지 않을 수 있습니다! 고마워, 작동 해. –