2

I am working on chat view. For this I have used this code :Chat Code This is working fine. Now I have used UIcollectionView in UItableViewCell. Collection view is working fine. But the issue is didselect method is not called of UICollectionView as well as UITableView. Please help me. I need your help very badly.

I have used this code in UITableViewCell Class:

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

In UIViewController I have used this code in 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;


}

Thanks in advance.

Sudha Tiwari
  • 2,499
  • 26
  • 50

3 Answers3

1

If your problem is that your UICollectionView taps do not cause the UITableViewCell it is in to be selected then you can bypass this by subclassing UICollectionView and modifying the hitTest function to your liking.

See my answer here.

This one lets you tap on anything outside of collection view items to select the table cell, but collection view items themselves will block the taps and process them as needed.

Community
  • 1
  • 1
William T.
  • 12,831
  • 4
  • 56
  • 53
1

Modifying the hitTest method wasn't working in my case. I decided to use 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
    }
}
hgwhittle
  • 9,316
  • 6
  • 48
  • 60
0

Don't forget to set delegate and dataSource.

self.tableView.delegate = self;
self.tableView.dataSource = self;
Pipiks
  • 2,018
  • 12
  • 27