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

     

  • 相关阅读:
    万丈高楼平地起
    @synthesis 使用的时候注意的地方
    arc4random()
    Whereami: CLLocationManager not calling delegate
    总结
    生病两日,真是难受
    Xcode 5 Error CertUIFramework.axbundle
    c++笔记 重要的声明
    class
    检查没有错误或警告
  • 原文地址:https://www.cnblogs.com/savagemorgan/p/3831546.html
Copyright © 2011-2022 走看看