ios - 将collectionview添加到uitableviewcell并为tableview的特定单元重新加载数据
问题描述
如何使用隐藏和取消隐藏功能将来自服务器的数据绑定到位于 uitableviewcell 内的 collectionview。点击 uibutton 我想让 colectionview 可见并在点击按钮时再次隐藏它。当我滚动 tableview 内的数据时会发生什么集合视图从一个单元格跳到另一个单元格,使集合视图为空白。
-(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
解决方案
推荐阅读
- c++ - 是否可以使用单个定义同时定义函数的 const 和常规版本?(使用模板、自动、decltype 等)
- c# - 为什么游戏对象蓝轴朝前,但枪在红轴上而不是蓝色轴上?
- java - 为什么Java中的JTextfield在运行时不显示文本?
- laravel - 为什么 laravel 中的电子邮件通知不起作用?
- powershell - 如何在 GitHub 工作流/操作中的 PowerShell 下运行名称中带有变量的脚本
- sql - Postgres - 从给定的时间戳获取午夜
- html - 使用 CSS 在 inline-block 元素后强制换行
- python-3.x - 如何将新文件放在硬盘驱动器上的某个位置(python)
- multithreading - 关于线程通信将任务从被调用的子线程发回主线程
- excel - 如果出现 MsgBox,则停止宏