2015-02-24 76 views
0

我在一個視圖控制器中有一個表視圖和一個聚集視圖。通過選擇單元格表視圖更改CollectionView中的內容

在表格視圖中,我具有圖像類別和集合視圖圖像的類別。

如何選擇單元格時更改爲集合視圖的內容?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    int numberFromArray=[numbers objectAtIndex:indexPath.row]; 
    NSString *link=[recipeImages objectAtIndex:indexPath.row]; 

    static NSString *identifier = @"Cell"; 


    NSString *imagineURL=[recipeImages objectAtIndex:4]; 

    rowIndex=indexPath.row+1 ; 
    NSString *strFromInt = [NSString stringWithFormat:@"%d",rowIndex]; 
    NSString * urlValue = [imagineURL stringByReplacingOccurrencesOfString:@"X" 
                   withString:strFromInt]; 




    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlValue]]; 
    recipeImageView.image = [UIImage imageWithData:imageData]; 

    [self.collection reloadData]; 


} 




- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *identifier = @"Cell"; 


    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    recipeImageView = (UIImageView *)[cell viewWithTag:100]; 


    // NSString *imagineURL=imageTest; 

    rowIndex=indexPath.row+1 ; 
    NSString *imagineURL=[recipeImages objectAtIndex:1]; 

    NSString *strFromInt = [NSString stringWithFormat:@"%d",rowIndex]; 
    NSString * urlValue = [imagineURL stringByReplacingOccurrencesOfString:@"X" 
                withString:strFromInt]; 




    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlValue]]; 
    recipeImageView.image = [UIImage imageWithData:imageData]; 



    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]]; 





    button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [button addTarget:self 
       action:@selector(getPicture:) forControlEvents:UIControlEventTouchDown]; 
    button.frame = recipeImageView.frame; 
    button.tag=indexPath.row; 
    [cell.contentView addSubview:button]; 



    return cell; 
} 

回答

0

您必須對(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath進行更改。更新您的圖像陣列並更新UICollectionView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.imagesCollection = //get you images from wherever 
    [self.collectionView reloadData]; 
} 
+0

這是changin只從集合中的一個圖像工作。我更新我的問題。 – 2015-02-24 17:03:30

0

這是一種正常和通常的邏輯。

在您的tableView的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath委託方法中,選擇單元格時放置選定的問題。

選擇一個你想要的UICollectionViewCell *cell = [myCollection cellForItemAtIndexPath:1]並做一些改變,如cell.text = @"Text had changed"。不要忘了最後打電話給reloadData

HTH。

+0

我想更改collectionView中的所有圖像 – 2015-02-24 17:09:41

+0

@NicoleSD第一個方法中的recipeImageView是什麼?似乎你沒有任何改變,只是調用'[self.collection reloadData]' – 2015-02-25 01:55:58