通过从tableview中选择一个单元格来更改Collection View中的内容

时间:2015-02-24 13:16:14

标签: ios objective-c uitableview uicollectionview

我在一个视图控制器中有一个表视图和一个集合视图。

在表格视图中,我有图像类别和集合视图图像。

如何在选择单元格时更改为集合视图的内容?

- (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;
}

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

这是正常和通常的逻辑。

在tableView的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath委托方法中,在选中单元格时输入所选问题。

选择您想要的UICollectionViewCell *cell = [myCollection cellForItemAtIndexPath:1]并执行更改,例如cell.text = @"Text had changed"。不要忘记最后致电reloadData

HTH。