zoukankan      html  css  js  c++  java
  • iPhone控件之UITableView

    一、UITableView的最基本使用

    实现协议UITableViewDelegate,UITableViewDataSource的下述方法:

    //设置表的分区数
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView 
    { 
        return 1; 
    }
    
    //设置每个区的行数
    - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section 
    {
        return [UIFont familyNames].count;
    }
    
    //设置cell
    - (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCellStyle style =  UITableViewCellStyleDefault;
        UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:@"BaseCell"];
        if (!cell) 
            cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:@"BaseCell"] autorelease];
        cell.textLabel.text = [[UIFont familyNames] objectAtIndex:indexPath.row];
        return cell;
    }
    
    //设置点击行的事件
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        NSString *font = [[UIFont familyNames] objectAtIndex:indexPath.row];
        [MAINLABEL setText:font];
        [MAINLABEL setFont:[UIFont fontWithName:font size:18.0f]];
    }
    //设置行高度
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 250;
    }

    二、使用一个view来实现表格行的自定义

    -(UITableViewCell*) tableView:(UITableView*)tableView  
            cellForRowAtIndexPath:(NSIndexPath*)indexPath
    {
        static NSString *SectionsTableIdentifier = @"SectionsTableIdentifer";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
        
        //if (cell==nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SectionsTableIdentifier];
        //}
        
        cell.textLabel.backgroundColor = [UIColor clearColor];  
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];   
        
        //homecellview用来设置单元表中的内容
        UIView *homecellview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 200)];
        
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imagesource objectAtIndex:indexPath.row]]];
        image.frame = CGRectMake(400, 50, 300, 185);
        
        
        UILabel *enname = [[UILabel alloc] initWithFrame:CGRectMake(50,20,300,60)];
        enname.font = [UIFont boldSystemFontOfSize:22];
        enname.backgroundColor = [UIColor clearColor];
        enname.textColor = [UIColor whiteColor];
        enname.text = [datasource objectAtIndex:indexPath.row];
        enname.numberOfLines = 0;
        [enname sizeToFit];
        
        
        UILabel *time = [[UILabel alloc] initWithFrame:CGRectMake(50,80,200,30)];
        time.backgroundColor = [UIColor clearColor];
        time.textColor = [UIColor whiteColor];
        
        
        UITextView *textview = [[UITextView alloc] initWithFrame:CGRectMake(50, 110, 300, 110)];
        textview.backgroundColor = [UIColor clearColor];
        textview.textColor = [UIColor whiteColor];
        textview.editable = NO;
        textview.scrollEnabled = NO;
        textview.font = [UIFont boldSystemFontOfSize:16];
        
        
        [homecellview addSubview:image];
        [homecellview addSubview:enname];
        [homecellview addSubview:time];
        [homecellview addSubview:textview];
        [cell.contentView addSubview:homecellview];
    
    time.text = @"2011-12-16";
                textview.text = @"  内容设置";
    
     [cell setSelectionStyle:UITableViewCellSelectionStyleGray];//设置选择行时的背景颜色
        [homecellview release];
        return cell;
    
    }

    三、使用系统的自带三个cell风格

    - (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCellStyle style;
        NSString *cellType;
        
        switch (indexPath.row % 4)
        {
            case 0:
                style = UITableViewCellStyleDefault;
                cellType = @"Default Style";
                break;
            case 1:
                style = UITableViewCellStyleSubtitle;
                cellType = @"Subtitle Style";
                break;
            case 2:
                style = UITableViewCellStyleValue1;
                cellType = @"Value1 Style";
                break;
            case 3:
                style =  UITableViewCellStyleValue2;
                cellType =  @"Value2 Style";
                break;
                
        }
        
        UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:cellType];
        if (!cell) 
            cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:cellType] autorelease];
        
        if (indexPath.row > 3) 
            cell.imageView.image = [UIImage imageNamed:@"icon.png"];
        
        cell.textLabel.text = cellType;
        cell.detailTextLabel.text = @"Subtitle text";
        return cell;
    }
  • 相关阅读:
    函数及习题
    数组和集合
    数组和集合实例
    普通集合和泛型集合的区别,哈希表和字典表的区别,队列和堆栈的区别以及堆和栈的区别。
    c#时间表示
    c#正则表达式
    js正则实例
    习题实例
    c#数据类型
    简单控件
  • 原文地址:https://www.cnblogs.com/foxmin/p/2444755.html
Copyright © 2011-2022 走看看