TableViewCell 里面嵌的TableViewCell 怎么样点击里面的TableViewCell进行

iOS 码拜 8年前 (2016-03-24) 968次浏览
下面是里面TableView的代码

#import "ClassViewCell.h"
#import "MyCollectionCell.h"
#import "StringManager.h"
#import "HomeViewController.h"
#import "FreeWorkList.h"
@implementation ClassViewCell 
#define SCREEN_WIDTH   [[UIScreen mainScreen] bounds].size.width
@synthesize names;
@synthesize images;
- (void)awakeFromNib {
    // Initialization code
    [self setUpCollection];
   
}
    
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}
-(void)setUpCollection{
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 200) collectionViewLayout:flowLayout];
    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;
    [self.collectionView setBackgroundColor:[UIColor clearColor]];
  
    
    
    names=@[@"兼职",@"培训",@"活动",@"实习",@"全职",@"假期工",@"在线兼职",@"公告"];
    images=@[@"jzicon.png",@"pxicon.png",@"hdicon.png",@"sxicon.png",@"qzicon.png",@"jqgicon.png",@"zxjzicon.png",@"ggicon.png"];
}
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"MyCollectionCell";
    UINib *cellNib = [UINib nibWithNibName:@"MyCollectionCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCollectionCell"];
    
    
    UICollectionViewCell *cell  = [self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell viewWithTag:2];
    UIImageView *imageVIew=(UIImageView *)[cell viewWithTag:1];
    
            label.text=[names objectAtIndex:indexPath.row];
            imageVIew.image=[UIImage imageNamed:[images objectAtIndex:indexPath.row]];
    return cell;
}
#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(70, 70);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if([[StringManager deviceString] isEqualToString:@"iPhone6"])
    {
        return UIEdgeInsetsMake(0, 30, 5, 20);
    }
    else if ([[StringManager deviceString] isEqualToString:@"iPhone6Plus"])
    {
        return UIEdgeInsetsMake(0, 30, 5, 20);
    }
    else
    {
        return UIEdgeInsetsMake(0, 10, 0, 0);
    }
    
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    //临时改变个颜色,看好,只是临时改变的。假如要永久改变,可以先改数据源,然后在cellForItemAtIndexPath中控制。(和UITableView差不多吧!O(∩_∩)O~)
    cell.backgroundColor = [UIColor clearColor];
    NSLog(@"走到这里了");
}
//返回这个UICollectionView能否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
@end

下面是主TableViewCell的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    
    if(indexPath.row==0){
        //NSLog(@"cellForRowAtIndexPath:%@",tmpStr2);
        UINib *cellNib = [UINib nibWithNibName:@"ClassViewCell" bundle:nil];
        [self.tbView registerNib:cellNib forCellReuseIdentifier:@"ClassViewCell"];
        TableViewCell *scell = [self.tbView dequeueReusableCellWithIdentifier:@"ClassViewCell"];
        scell.contentView.backgroundColor = [UIColor clearColor];
        UIView *backgroundView = [[UIView alloc] initWithFrame:scell.frame];
        backgroundView.backgroundColor = [UIColor clearColor];
        scell.selectedBackgroundView = backgroundView;
        return scell;
    }else if(indexPath.row==1){
        UINib *cellNib = [UINib nibWithNibName:@"InfoTableViewCell" bundle:nil];
        [self.tbView registerNib:cellNib forCellReuseIdentifier:@"InfoTableViewCell"];
        TableViewCell *scell = [self.tbView dequeueReusableCellWithIdentifier:@"InfoTableViewCell"];
        UIView *backgroundView = [[UIView alloc] initWithFrame:scell.frame];
        backgroundView.backgroundColor = [UIColor clearColor];
        scell.selectedBackgroundView = backgroundView;
        scell.backgroundColor=[UIColor clearColor];
        return scell;
    }else{
        
        static NSString *CellTableIndentifer=@"TableViewcell";
        
        
        UINib *cellNib = [UINib nibWithNibName:@"TableViewCell" bundle:nil];
        [self.tbView registerNib:cellNib forCellReuseIdentifier:@"TableViewCell"];
        TableViewCell *scell = [self.tbView dequeueReusableCellWithIdentifier:@"TableViewCell"];
        
        UIView *backgroundView = [[UIView alloc] initWithFrame:scell.frame];
        backgroundView.backgroundColor = SELECTED_BACKGROUND;
        scell.selectedBackgroundView = backgroundView;
        if(fwids.count>=8)
        {
        
        scell.title=[fwnames objectAtIndex:indexPath.row-2];
        scell.content=[fwcons objectAtIndex:indexPath.row-2];
        scell.content2=[fwcon2s objectAtIndex:indexPath.row-2];
        scell.money=[fwmoneys objectAtIndex:indexPath.row-2];
        scell.city=[fwcitys objectAtIndex:indexPath.row-2];
        scell.iconsrc=[UIImage imageNamed:[images objectAtIndex:indexPath.row-2]];
        }
     return scell;
    }
    
    
}
解决方案

50

你的结构是:最外面有一个tableVIew,然后 tableView 的每一个 Cell 里都有一个 CollectionView 吗?
假如是这样的话,给 Cell 加一个回调,建议用 Block回调,然后把相关信息带出去,在 tableView 所在的 Controller 里做跳转。

50

你的UITableViewCell里又封装了一个UICollectionView. 你是想点击UICollectionVIew上的cell来实现页面跳转对吧。 假如是那样的话,你可以用两种方式,一是使用delegate, 二是使用block 。熟悉哪个就用哪个,这里可以以delegate为例简单说一下
1. 在你的这个自定义UITableviewCell中,先创建一个delegate. 并编写你的协议方法

@protocol  ClassViewCellDelegate <NSObject>
 -(void)classViewCell:(ClassViewCell *)classViewCell didSelectWithItem:(id)item;  //////假如有回调的数据,可以通过item来传递
@end

2. 在ClassViewCellDelegate的头文件中增加一个属性用于声明delegate

@interface ClassViewCell:UITableViewCell 
@property (nonatomic,assign) id<ClassViewCellDelegate> delegate;
@end

3. delegate 中方法是由点击collectionViewCell 时触发的。所以在

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
       //////除了你原有的方法外,再加上如下代码,交由代理者来处理
       if ([self.delegate respondToSelector:@seletor(classViewCell:didSelectWithItem:)]) {
                      [self.delegate classViewCell:self didSelectWithItem:nil]; /////假如需要传递值,将值通过第二个参数传递,假如没有,设置为nil
       }
}

4. 在外部使用 ClassViewCell的这个控制器上 实现定义的协议,如

@interface ViewController: UIViewController <ClassViewCellDelegate >

5. 在这个控制器中实现协议中的方法,在这里就可以处理你的跳转操作

-(void)classViewCell:(ClassViewCell *)classViewCell didSelectWithItem:(id)item {
           UIViewController *vc = [UIViewController new];
          //////假设是通过UINavigationController来导航的视图
          [self.navigationController pushViewController:vc animated:YES]; 
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明TableViewCell 里面嵌的TableViewCell 怎么样点击里面的TableViewCell进行
喜欢 (0)
[1034331897@qq.com]
分享 (0)