zoukankan      html  css  js  c++  java
  • UITableView 应用及其总结


    Plain:

    Grouped:

    Cell的结构图:
    点击看全图

    UITableViewCellStyleDefault:预设使用这种,若左侧ImageView没图的话,只有一行字(textLable.text)。

    UITableViewCellStyleValue1:左侧为textLable.text并且左对齐,右侧为detailTextLable.text并且右对齐。

    UITableViewCellStyleValue2:左侧为detailTextLable.text,右侧为textLable.text并且左对齐。

    UITableViewCellStyleSubtitle:跟UITableViewCellStyleDefault大致相同,detailTextLable.text出现在textLable.text下方。

    如何使用:

     

        - (void)dealloc  
        {  
             [itemsArray release];  
             [super dealloc];  
        }  
          
        - (void)viewDidLoad  
        {  
           [super viewDidLoad];  
          
           //初始化资料阵列,待会使用  
           NSMutableArray *itemsArray = [[NSArray alloc] initWithObjects:@"row 1",@"row 2",@"row 3",nil];  
          
        }  
          
        #pragma mark - Table view data source  
          
        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
        {  
          
            // Return the number of sections.  
            // 告诉tableView总共有多少个section需要显示  
            return 1;  
        }  
          
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
        {  
          
            // Return the number of ro​​ws in the section.  
            // 告诉tableView一个section里要显示多少行  
            return [itemsArray count];  
        }  
          
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
        {  
            //cell的标饰符  
            static NSString *CellIdentifier = @"cellIdentifier";  
          
            //指定tableView可以重用cell,增加性能,不用每次都alloc新的cell object  
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
          
            //如果cell不存在,从预设的UITableViewCell Class里alloc一个Cell object,应用Default样式,你可以修改为其他样式  
            if (cell == nil) {  
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
            }  
          
            // Configure the cell...  
          
            //每一行row进来都判定一下,分别依次选用不同的图片  
            switch (indexPath.row) {  
                case 0:  
                {  
                    cell.imageView.image = [UIImage imageNamed:@"image0.png"];  
                }  
                    break;  
                case 1:  
                {  
                    cell.imageView.image = [UIImage imageNamed:@"image1.png"];  
                }  
                    break;  
                case 2:  
                {  
                    cell.imageView.image = [UIImage imageNamed:@"image2.png"];  
                }  
                    break;  
                default:  
                {  
                    cell.imageView.image = [UIImage imageNamed:@"default.png"];  
                }  
                    break;  
            }  
          
            //其他相同的属性一并设定  
            cell.textLabel.text = [itemsArray objectAtIndex:indexPath.row];  
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];  
          
            //设字体、颜色、背景色什么的  
            cell.textLabel.backgroundColor = [UIColor clearColor];  
            cell.detailTextLabel.backgroundColor = [UIColor clearColor];  
            cell.textLabel.textColor = [UIColor colorWithRed:54.0/255.0 green:161.0/255.0 blue:219.0/255.0 alpha:1];  
            cell.detailTextLabel.textColor = [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1];  
          
            //设定textLabel的最大允许行数,超过的话会在尾未以...表示  
            cell.textLabel.numberOfLines = 2;  
          
            return cell;  
        }  
          
        //这个是非必要的,如果你想修改每一行Cell的高度,特别是有多行时会超出原有Cell的高度!  
        -(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
        {  
            return 85.0;  
        }  

     

    如果比较进阶一点的,想修改或者增加更多的component进去Cell里面,有两种方法(大同小异):
    第一种是在cellForRowAtIndexPath delegate method里alloc cell时在contentView里面addSubView。

    第二种是需要继承Cell作一个Custom Cell 的Class,并可以使用layoutSubview等方法来修改component的frame呢。

    http://blog.csdn.net/titer1991/article/details/7945127

     

  • 相关阅读:
    如何用视频云技术,搞一个爆红的 “反应视频” 项目?
    停车场事故频频,AI 达人将摄像头变身安全卫士
    WebRTC 传输安全机制第二话:深入显出 SRTP 协议
    阿里云视频云 Retina 多媒体 AI 体验馆开张啦!
    20 岁发表 SCI 的学霸,梦想用算法改变世界
    阿里绩效考核,简单到不可思议,员工死心塌地跟你干!(转)
    【官方】阿里巴巴合伙人制度全文(转)
    blob视频地址如何下载(转)
    软件开发项目规划时,SA、SD与SE的区别与重要性 【转】
    一分钟看懂公有云和私有云的区别
  • 原文地址:https://www.cnblogs.com/savagemorgan/p/3831546.html
Copyright © 2011-2022 走看看