iPhone控件之UITableView

本文详细介绍了如何在iOS开发中使用UITableView实现基本功能、自定义行视图以及利用系统自带三种不同风格的cell,包括设置分区数、行数、cell样式、点击事件和高度,同时展示了如何使用自定义视图作为单元格内容。

一、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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值