uiTableviewcell 中未调用 UicollectionView didSelectItemAtIndexPath
Posted
技术标签:
【中文标题】uiTableviewcell 中未调用 UicollectionView didSelectItemAtIndexPath【英文标题】:UicollectionView didSelectItemAtIndexPath is not called in uiTableviewcell 【发布时间】:2016-03-10 10:31:59 【问题描述】:我正在处理聊天视图。为此,我使用了此代码:Chat Code 这工作正常。现在我在 UItableViewCell 中使用了 UIcollectionView。集合视图工作正常。但问题是没有调用 UICollectionView 和 UITableView 的 didselect 方法。请帮我。我非常需要你的帮助。
我在 UITableViewCell 类中使用过这段代码:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
self.backgroundColor = [UIColor clearColor];
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0f)
self.textLabel.backgroundColor = [UIColor whiteColor];
self.textLabel.font = [UIFont systemFontOfSize:14.0f];
self.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.textLabel.numberOfLines = 0;
self.textLabel.textAlignment = NSTextAlignmentLeft;
self.textLabel.textColor = [UIColor blackColor];
_timestampLabel = [[UILabel alloc] init];
_timestampLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_timestampLabel.textAlignment = NSTextAlignmentCenter;
_timestampLabel.backgroundColor = [UIColor clearColor];
_timestampLabel.font = [UIFont systemFontOfSize:12.0f];
_timestampLabel.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1.0];
_timestampLabel.frame = CGRectMake(0.0f, 12, self.bounds.size.width, 18);
[self.contentView addSubview:_timestampLabel];
messageBackgroundView = [[UIImageView alloc] initWithFrame:self.textLabel.frame];
[self.contentView insertSubview:messageBackgroundView belowSubview:self.textLabel];
self.AvatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5,10+TOP_MARGIN, 50, 50)];
[self.contentView addSubview:self.AvatarImageView];
CALayer * l = [self.AvatarImageView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:self.AvatarImageView.frame.size.width/2.0];
self.selectionStyle = UITableViewCellSelectionStyleNone;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[self.collectionView registerNib:[UINib nibWithNibName:@"ImageCollection" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
[self.collectionView setUserInteractionEnabled:YES];
self.collectionView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:self.collectionView];
[messageBackgroundView setUserInteractionEnabled:YES];
[self.contentView setUserInteractionEnabled:YES];
// UITapGestureRecognizer *lpgr // = [[UITapGestureRecognizer alloc] // initWithTarget:self action:@selector(tapRecognized:)]; // lpgr.numberOfTapsRequired
= 1; // lpgr.delegate = self; // [self.collectionView addGestureRecognizer:lpgr];
[self setUserInteractionEnabled:YES];
return self;
在 UIViewController 中,我在 tableView cellForRowAtIndexPath 中使用了这段代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *L_CellIdentifier = @"SPHTextBubbleCell";
self.documentsArray = [[NSMutableArray alloc]init];
self.documentsArray = [[self.messagesArray objectAtIndex:indexPath.row]valueForKey:@"Attachments"];
SPHTextBubbleCell *cell = [tableView dequeueReusableCellWithIdentifier:L_CellIdentifier];
cell.userInteractionEnabled = YES;
if (cell == nil)
cell = [[SPHTextBubbleCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:L_CellIdentifier];
// cell.bubbletype=(([[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:@"MessageFromId"]intValue] == 1))?@"LEFT":@"RIGHT";
if ([[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:@"MessageFromId"]intValue] == 1)
cell.bubbletype = @"RIGHT";
else
cell.bubbletype = @"LEFT";
[cell setBackgroundColor:[UIColor clearColor]];
cell.textLabel.text = [[self.messagesArray objectAtIndex:indexPath.row]valueForKey:@"Content"];
cell.textLabel.tag=indexPath.row;
NSString *dateString = [NSString stringWithFormat:@"%@",[self mfDateFromDotNetJSON:[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:@"UpdatedOn"]]];
NSLog(@"dateString..%@",dateString);
NSString *myString = [NSString stringWithFormat:@"%@",dateString];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss ZZZ";
NSDate *yourDate = [dateFormatter dateFromString:myString];
// NSTimeZone *utc = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
// [dateFormatter setTimeZone:utc];
dateFormatter.dateFormat = @"dd-MMM-yy HH:mm";
NSString *newString = [dateFormatter stringFromDate:yourDate];
NSLog(@"newString..%@",newString);
if ([self.documentsArray count]!=0)
cell.CustomDelegate = self;
[cell.collectionView setUserInteractionEnabled:YES];
[cell.collectionView setDelegate:self];
[cell.collectionView setDataSource:self];
cell.collectionView.delegate =self;
cell.collectionView.dataSource = self;
[cell.collectionView reloadData];
[cell.collectionView setHidden:NO];
[cell.collectionView setBackgroundColor:[UIColor clearColor]];
else
[cell.collectionView setHidden:YES];
cell.collectionView.tag =indexPath.row;
cell.timestampLabel.text = newString;
[cell.AvatarImageView sd_setImageWithURL:([[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:@"MessageFromId"]intValue] == 1)?[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",imageURLLive,[[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:@"SellerPicture"] valueForKey:@"PictureUrl"]]]:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",imageURLLive,[[[self.messagesArray objectAtIndex:indexPath.row]valueForKey:@"BuyerPicture"] valueForKey:@"PictureUrl"]]]
placeholderImage:[UIImage imageNamed:@"Nav-profile1.png"]];
return cell;
提前致谢。
【问题讨论】:
请将 Delegate 和 DataSource 添加到您的视图控制器。 我已经加了:( 请您发布一些代码。所以我会帮你解决问题。 当然.... 我在 UITableViewCell 中创建了 UICollectionView 我建议你不要在复杂的应用程序中使用第三方库。 【参考方案1】:如果您的问题是您的 UICollectionView 轻按不会导致它所在的 UITableViewCell 被选中,那么您可以通过继承 UICollectionView 并根据自己的喜好修改 hitTest 函数来绕过此问题。
See my answer here.
这个可以让你点击集合视图项目之外的任何东西来选择表格单元格,但集合视图项目本身会阻止点击并根据需要处理它们。
【讨论】:
【参考方案2】:修改hitTest
方法在我的情况下不起作用。我决定使用UITapGestureRecognizer
。
// Custom UITableViewCell
override func awakeFromNib()
super.awakeFromNib()
let tapGR = UITapGestureRecognizer(target: self, action: #selector(collectionViewTapped(_:)))
tapGR.numberOfTapsRequired = 1
self.collectionView.addGestureRecognizer(tapGR)
func collectionViewTapped(gr: UITapGestureRecognizer)
let point = gr.locationInView(self.collectionView)
if let indexPath = self.collectionView.indexPathForItemAtPoint(point)
// Do stuff
【讨论】:
【参考方案3】:别忘了设置委托和数据源。
self.tableView.delegate = self;
self.tableView.dataSource = self;
【讨论】:
我已经设置了委托和数据源:( 也许 self.tableView.allowsSelection = YES;和 self.collectionView.allowsSelection = YES;以上是关于uiTableviewcell 中未调用 UicollectionView didSelectItemAtIndexPath的主要内容,如果未能解决你的问题,请参考以下文章
.m 文件中未访问 UITableViewCell 中的标签