如何使用隐藏和取消隐藏功能将服务器中的数据绑定到uitableviewcell内的collectionview。在uibutton上点击我想使colectionview可见并在按钮的点击上再次隐藏它。发生的事情是当我滚动tableview collectionview中的数据会从一个单元格跳到另一个单元格,从而使collection view为空白。
-(void)arrowButtonTapped:(UIButton *)donwbtn{
myob = [NSString stringWithFormat:@"%li", (long)donwbtn.tag];
NSLog(@"%@",myob);
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0
inSection:donwbtn.tag];
OppCell* celll = [_tableView cellForRowAtIndexPath:indexPath];
celll.collView.tag=donwbtn.tag;
if ([checkcoll containsObject:myob]) {
[checkcoll removeObject:myob];
celll.collView.hidden = true;
celll.collvwhght.constant=0;
celll.colltop.constant=0;
celll.collbottom.constant=0;
}
else
{
[checkcoll addObject:myob];
celll.collView.hidden = false;
celll.collvwhght.constant=229;
celll.colltop.constant=9;
celll.collbottom.constant=8;
[celll.collView reloadData] ;
}
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
OppCell *cell = (OppCell *)[tableView
dequeueReusableCellWithIdentifier:@"OppCell"];
NSString *theIndexpath = [NSString stringWithFormat:@"%ld",
(long)indexPath.section];
if ([checkcoll containsObject:theIndexpath])
{
cell.collView.hidden = false;
cell.collvwhght.constant=229;
cell.colltop.constant=9;
cell.collbottom.constant=8;
if((![[[_Activitylistarray
objectAtIndex:indexPath.section]
valueForKey:@"OpportunityContactInfoList"]
isKindOfClass:[NSNull class]])&&([[[_Activitylistarray
objectAtIndex:indexPath.section]
valueForKey:@"OpportunityContactInfoList"]count]>0)){
if(cell.contactdetailsarray == nil) {
cell.contactdetailsarray =[[NSMutableArray alloc]init];
for(NSDictionary * contactdict in
[[_Activitylistarray objectAtIndex:indexPath.section]
valueForKey:@"OpportunityContactInfoList"] ){
[cell.contactdetailsarray addObject:contactdict];
[cell.collView reloadData];
}
}
}
}
else
{
cell.collView.hidden = true;
cell.collvwhght.constant=0;
cell.colltop.constant=0;
cell.collbottom.constant=0;
// [cell.contactdetailsarray removeAllObjects];
// [cell.collView reloadData];
}
////Code in UITableViewCell
#import "OppCell.h"
#import "OppsContactCell.h"
@implementation OppCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[self.collView registerNib:[UINib nibWithNibName:@"OppsContactCell"
bundle:nil] forCellWithReuseIdentifier:@"OppsContactCell"];
self.collView.pagingEnabled = YES;
UICollectionViewFlowLayout * layout=[[UICollectionViewFlowLayout
alloc]init];
layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
self.collView.collectionViewLayout = layout;
self.collView.decelerationRate =
UIScrollViewDecelerationRateNormal;
// _contactdetailsarray=[NSMutableArray array];
// self.collvwhght.constant=0;
//self.collView.hidden=YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
OppsContactCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"OppsContactCell"
forIndexPath:indexPath];
cell.contactimgvw.image = [cell.contactimgvw.image
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[cell.contactimgvw setTintColor:[UIColor blackColor]];
cell.numberimgvw.image = [cell.numberimgvw.image
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[cell.numberimgvw setTintColor:[UIColor blackColor]];
cell.emailimgvw.image = [cell.emailimgvw.image
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[cell.emailimgvw setTintColor:[UIColor blackColor]];
if(_contactdetailsarray.count==1){
if(([[[_contactdetailsarray
objectAtIndex:0]valueForKey:@"Designation"]isEqualToString:@""])&&([[[_contactdetailsarray
objectAtIndex:0]valueForKey:@"Department"]isEqualToString:@"" ])){
cell.lbldesignation.text=@"";
}
else{
cell.lbldesignation.text=[NSString stringWithFormat:@"%@,%@",
[[_contactdetailsarray objectAtIndex:0]valueForKey:@"Designation"],
[[_contactdetailsarray objectAtIndex:0]valueForKey:@"Department"]];
}
cell.lblcontatname.text=[[_contactdetailsarray
objectAtIndex:0]valueForKey:@"ContactName"];
cell.lblcontactnumber.text=[[_contactdetailsarray
objectAtIndex:0]valueForKey:@"TelephoneNo"];
cell.lblemail.text=[[_contactdetailsarray
objectAtIndex:0]valueForKey:@"EmailAddress"];
}
else{
if(([[[_contactdetailsarray
objectAtIndex:0]valueForKey:@"Designation"]isEqualToString:@""])&& .
([[[_contactdetailsarray
objectAtIndex:0]valueForKey:@"Department"]isEqualToString:@"" ])){
cell.lbldesignation.text=@"";
}
else{
cell.lbldesignation.text=[NSString stringWithFormat:@"%@,%@",
[[_contactdetailsarray
objectAtIndex:indexPath.section]valueForKey:@"Designation"],
[[_contactdetailsarray
objectAtIndex:indexPath.section]valueForKey:@"Department"]];
}
cell.lblcontatname.text=[[_contactdetailsarray
objectAtIndex:indexPath.row]valueForKey:@"ContactName"];
cell.lblcontactnumber.text=[[_contactdetailsarray
objectAtIndex:indexPath.row]valueForKey:@"TelephoneNo"];
cell.lblemail.text=[[_contactdetailsarray
objectAtIndex:indexPath.row]valueForKey:@"EmailAddress"];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(_collView.bounds.size.width
,_collView.bounds.size.height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 5;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section{
// return 5;
if(_contactdetailsarray.count==0){
return 0;
}
else{
return _contactdetailsarray.count;
}
}
感谢与问候, Roshan.k