2012-09-07 26 views
1

我想從NSMutableArray添加圖片到tableview中的自定義單元格。每個單元格中將有四個或更多圖像。因此,這裏是我的問題:如何添加多個圖片到具有標籤或標識符的自定義tableviewcells

如果我指的是indexPath.row定製細胞看起來像這些:

小區1:圖片1,圖片2,圖片3,圖片4
小區2:圖片2,圖片3,圖片4,圖片5
小區3:圖片3,圖片4,圖片5,圖片6

,但我想:

小區1:圖片1,圖片2,圖片3,圖片4
小區2:圖片5,圖片6,圖片7,圖片8
小區3:圖片9,圖片10,圖片11,圖片12

我是新來的Xcode和我不看到很好的解決方案。

代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"ImageCustomCell"; 

    ImageCustomCell *cell = (ImageCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ImageCustomCell" owner:nil options:nil]; 

     for(id currentObject in topLevelObjects) { 

      if ([currentObject isKindOfClass:[UITableViewCell class]]) { 

       cell = (ImageCustomCell *) currentObject; 

      break; 
      } 
     } 
    } 
// here i get the indexPath.row for every cell and add +1 - +3 to it to get the next image, but every new cell just get +1, not +4 
    cell.imageForCell.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:indexPath.row]]; 
    cell.imageForCell2.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:indexPath.row+1]]; 
    cell.imageForCell3.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:indexPath.row+2]]; 
    cell.imageForCell4.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:indexPath.row+3]]; 
    return cell; 
} 

回答

1

你的問題是,雖然indexPath.row通過增加每次調用一個tableView:cellForRowAtIndexPath:你的編碼,如果它增加了4.當然,UITableView行索引增量應該和將保持一個在每排,所以你必須找到不同的方法。

您需要找到一個函數,將indexPath映射到imagePathArray中的索引,該索引對應於要放入單元格中indexPath所述行的最左邊圖像。一旦找到該索引,剩下的三張圖像就只有1,2和3個元素。

由於這沒有標記爲「家庭作業」,我想我會給你答案:這是行數乘以每行圖像的數量。你可以隨心所欲,或者使用這一點代碼。尚未編譯或測試,如果你不能自己解決任何錯誤或錯誤,請告訴我。

這樣的實現方法:

- (NSArray *)imagePathsForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSInteger imagePathArrayStartingIndex = indexPath.row * IMAGES_PER_ROW; 
    NSRange imagePathArrayIndexRange = NSMakeRange(imagePathArrayStartingIndex, IMAGES_PER_ROW); 
    NSIndexSet *imagePathArrayIndexes = [NSIndexSet indexSetWithIndexesInRange:imagePathArrayIndexRange]; 
    NSArray *imagePathsForRow = [imagePathArray objectsAtIndexes:imagePathArrayIndexes]; 
    return imagePathsForRow; 
} 

然後改變,你在tableView:cellForRowAtIndexPath設置你的細胞圖像的線條:

NSArray *imagePathsForRow = [self imagePathsForRowAtIndexPath:indexPath]; 
cell.imageForCell.image = [UIImage imageWithContentsOfFile:[imagePathsForRow objectAtIndex:0]]; 
cell.imageForCell2.image = [UIImage imageWithContentsOfFile:[imagePathsForRow objectAtIndex:1]]; 
cell.imageForCell3.image = [UIImage imageWithContentsOfFile:[imagePathsForRow objectAtIndex:2]]; 
cell.imageForCell4.image = [UIImage imageWithContentsOfFile:[imagePathsForRow objectAtIndex:3]]; 

希望這有助於!

+0

太容易了!謝謝! – myr0

+0

不客氣!這有助於解決問題嗎?還是需要進一步的幫助? –

+0

是的,非常幫助我。但它更簡單,然後:cell.imageForCell.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:(indexPath.row * 1)]];和 cell.imageForCell.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:(indexPath.row * 2)]];等等.. – myr0

相關問題