• UITableView的嵌套使用


      本文原创,转载请注明出处!!  

      紧赶慢赶,拼了命的去学习工作,还是没有完成......伤心啊,自己的技术是在太差了,要学的东西太多,现在要做一个大一点的项目就显得力不从心了。

      先把工作扔一边吧,虽然自己学的东西不多,不过还是把自己觉得有用的东西分享出来给大家看,让大家能够更快的学习,有时间多去学习学习,就不会像小白猪这样提笔忘词了。

      UITableView是cocoa框架中使用的非常多的一个控件,相比其他控件还是有点复杂的,下面分享自己做的一个UITableView的嵌套的例子,效果是一个纵向的table,每一个节点里面又有一个横向的table,可以横向纵向滑动,类似土豆网iphone版的视频列表。

    效果图如下:

     

    其实原理非常简单,先定义一个普通的UITableView,设置它的行数和节点数。

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 1;//每个节点只有一行
    }
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 7;//7个节点
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 140;//设置每一个节点的高
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([indexPath section]==0) {
            NSLog(@"indexPath section%d",[indexPath section]);
            NSLog(@"rows === %d",[indexPath row]);//在这里可以添加点击触发执行的方法
        }
    }

    下面要注意了,是重点,作用是将每一个节点里都添加一个UITableView

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellname = @"Cell";
        InfoCell *cell = (InfoCell *)[tableView dequeueReusableCellWithIdentifier:cellname];//InfoCell是一个类,在里面重新定义了横向的UITableView,下面有介绍
        if (cell == nil){
        cell = [[[InfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellname number:0]autorelease];
            }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }

    下面是InfoCell这个类的.m文件里的代码,不是很难,重点我会说明。

    下面的方法中我只添加了_dataArray1,其实还有_dataArray2 ,_dataArray3,为了节省空间代码省去了。

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            
            tableSection = [passValue passValue_];
     //       porsection = tableSection.num;
            hortable = [[UITableView alloc]initWithFrame:CGRectMake(100, -90, 140, 320) style:UITableViewStylePlain];
            hortable.delegate = self;
            hortable.dataSource = self;
            [self addSubview:hortable];
            hortable.transform = CGAffineTransformMakeRotation(M_PI / 2 *3);//这里是将table旋转270度,如果是90度的话,相当于整个横向的UITableView也反了。
            NSString  *str = nil;
            _dataArray1 = [[NSMutableArray alloc] initWithCapacity:0];
            str = @"小白猪是头大笨猪!!!!";
            [_dataArray1 addObject:str];
            [_dataArray1 addObject:str];
            [_dataArray1 addObject:str];//在数组里添加了一段字符串,在table中显示,各位可以在这里给每一行添加不同的字符串。不过需要做个判断
        }
        return self;
    }

    接下来是设置具体的每一行,这里笨猪没有想到好的方法,因为cell会重用,我传了一个参数porsection来横向的table在纵向的table中的是在第几个节点的位置。如果有高手的话,请给出更好的方法,感激不尽。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
            static NSString *CellIdentifier1 = @"cell1";//每一行定义取不同的名字来取消重用。
        static NSString *CellIdentifier2 = @"cell2";
        static NSString *CellIdentifier3 = @"cell3";
        static NSString *CellIdentifier4 = @"cell4";
        static NSString *CellIdentifier5 = @"cell5";
        static NSString *CellIdentifier6 = @"cell6";
        static NSString *CellIdentifier7 = @"cell7";
        
            switch (porsection) {
                case 0:
                    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease];
                    cell.transform = CGAffineTransformMakeRotation(M_PI/2); //将cell中的文字旋转90度
                    [[cell textLabel] setText:[_dataArray1 objectAtIndex:indexPath.row]];
                    cell.textLabel.lineBreakMode = UILineBreakModeClip;
                    cell.textLabel.numberOfLines = 0;
    
                    break;
                case 1:
                    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease];
                    cell.transform = CGAffineTransformMakeRotation(M_PI/2);
                    [[cell textLabel] setText:[_dataArray2 objectAtIndex:indexPath.row]];
                    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
                    cell.textLabel.numberOfLines = 0;
                    break;
                    
                case 2:
                    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier3]autorelease];
                    cell.transform = CGAffineTransformMakeRotation(M_PI/2);
                    [[cell textLabel] setText:[_dataArray3 objectAtIndex:indexPath.row]];
                    break;
    
                case 3:
                    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier4]autorelease];
                    cell.transform = CGAffineTransformMakeRotation(M_PI/2);
                    [[cell textLabel] setText:[_dataArray4 objectAtIndex:indexPath.row]];
                    break;
    
                case 4:
                    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier5]autorelease];
                    cell.transform = CGAffineTransformMakeRotation(M_PI/2);
                    [[cell textLabel] setText:[_dataArray5 objectAtIndex:indexPath.row]];
                    break;
    
                case 5:
                    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier6]autorelease];
                    cell.transform = CGAffineTransformMakeRotation(M_PI/2);
    
                    [[cell textLabel] setText:[_dataArray6 objectAtIndex:indexPath.row]];
                    cell.textLabel.lineBreakMode = UILineBreakModeClip;
                    break;
                case 6:
                    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier7]autorelease];
                    cell.transform = CGAffineTransformMakeRotation(M_PI/2);
    
                    [[cell textLabel] setText:[_dataArray7 objectAtIndex:indexPath.row]];
                    break;
                default:
                    break;
            }
    
        return cell;
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 200;
    }

    还有一个困扰我的问题,我给每一行都添加不同的数据,为什么从第5行开始就重复第一行的内容,第6行重复第2行的,依此类推,我总是想不通,望高手指明。

     
  • 相关阅读:
    Django组件——分页器
    Django与Ajax
    Python常用模块——包&跨模块代码调用
    Python常用模块——正则表达式re模块
    Python常用模块——文件复制模块shutil
    Python常用模块——hashlib加密
    git小乌龟配置
    设计模式学习(27)- MVC模式
    设计模式学习(26)- 访问者模式
    设计模式学习(25)- 模板模式
  • 原文地址:https://www.cnblogs.com/xiaobaizhu/p/UITableView.html
Copyright © 2020-2023  润新知