UITableView和UICollectionView交互ios?

时间:2014-03-05 11:39:45

标签: ios uitableview uicollectionview

任何人都可以帮助我实现这样的目标 - (针对iPad iOS7开发)在UIViewController我有UITableView& UICollectionView(例如splitViewController)。当我点击UICollectionView时,我只想更改UITableViewCell中的图片。(我使用button而不是tableViewCell并在NSInteger)中收集索引。

请帮助我实现这一点,我这么多天都在努力,但直到现在都无法实现。 :(

这是我到目前为止所做的代码。

在我的viewDidLoad

self.collectionViewImages=[[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",nil] ;

然后在CollectionViewMethods

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
     return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{

    return [_collectionViewImages count];

}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

   UICollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"
                                forIndexPath:indexPath];


   NSString *collectionViewImageName=[self.collectionViewImages objectAtIndex:indexPath.row];
   UIImageView * collectionImage = (UIImageView*)[myCell viewWithTag:11];

  collectionImage.image =[UIImage imageNamed:collectionViewImageName];

 return myCell;
}

然后在ButtonClick(ofTableViewCell)上 - (我正在收集NSInteger中每个按钮的标记,并在numberOfItemsInSection中使用该属性,

-(void)btnTapped:(id)sender{
int tag= [(UIButton *)sender tag];
NSLog(@"Button Pressed%d",tag);
_buttonIndex=tag;
//_buttonIndex is NSInteger property
//If i add object in NSMutableArray and call reloadData my UI hangs here only.
//If  I empty my NSMutableArray and add new objects in it and then call reloadData, I am getting NSRangeException. 
}

现在我在numberOfItemsInSection中使用此属性作为...

if (_buttonIndex==1){

     [collectionImage setImage:[UIImage imageNamed:[_array1 objectAtIndex:indexPath.row]]];
     //My _array1 has 4 objects and my collectionViewImages array has 7 objects.

    }

我收到错误消息

*** Terminating app due to uncaught exception 'NSRangeException', 
reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]'

1 个答案:

答案 0 :(得分:0)

崩溃是因为你试图从索引4的数组中获取一个项目。但是只有索引:0,1,2,3。

在线检查array1的内容(如果它崩溃了):

[collectionImage setImage:[UIImage imageNamed:[_array1 objectAtIndex:indexPath.row]]];

否则找到你呼叫objectAtIndex的另一个地方,并检查它是否在那里崩溃。

相关问题