UicollectionView didSelectItemAtIndexPath未在uiTableviewcell中调用

时间:2016-03-10 10:32:00

标签: objective-c uitableview uicollectionview didselectrowatindexpath

我正在处理聊天视图。为此,我使用了以下代码:Chat Code 这工作正常。现在我在UItableViewCell中使用了UIcollectionView。集合视图工作正常。但问题是doselect方法没有调用UICollectionView以及UITableView。请帮我。我非常需要你的帮助。

我在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;


}

提前致谢。

3 个答案:

答案 0 :(得分:1)

如果您的问题是您的UICollectionView点击不会导致UITableViewCell被选中,那么您可以通过继承UICollectionView并根据自己的喜好修改hitTest函数来绕过它。

See my answer here.

这个允许您点击集合视图项之外的任何内容来选择表格单元格,但集合视图项本身将阻止点击并根据需要处理它们。

答案 1 :(得分:1)

在我的案例中,修改%Super.Context = ""; 方法并不起作用。我决定使用hitTest

UITapGestureRecognizer

答案 2 :(得分:0)

不要忘记设置delegate和dataSource。

self.tableView.delegate = self;
self.tableView.dataSource = self;