zoukankan      html  css  js  c++  java
  • FirstApp,iphone开发学习总结8,自定义TablevViewCell

    创建TableViewCell文件(继承于UITableViewCell),在.h文件中添加2个label和一个方法:

    @interface TableViewCell : UITableViewCell{
        UILabel *nameLbl;
        UILabel *ageLbl;
    }
    - (void)setValue:(NSString *)name age:(NSString *)age;

     创建子视图://增加了2个label

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            nameLbl = [[UILabel alloc] initWithFrame:CGRectZero];
            [[self contentView] addSubview:nameLbl];
            [nameLbl release];
            
            ageLbl = [[UILabel alloc] initWithFrame:CGRectZero];
            [[self contentView] addSubview:ageLbl];
            [ageLbl release];
        }
        return self;
    }

     子视图布局://只做了简单的处理,设置位置、改变字体大小和字体颜色

    //此处layoutSubviews方法是得到view的具体大小后,再实现子类布局,这里直接设置了。

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        nameLbl.frame = CGRectMake(5.05.020.020.0);
        nameLbl.font = [UIFont fontWithName:@"" size:18];
        nameLbl.textColor = [UIColor redColor];
        
        ageLbl.frame = CGRectMake(50.05.020.020.0);
        ageLbl.font = [UIFont fontWithName:@"" size:12];
        ageLbl.textColor = [UIColor blueColor];
    }

     设置方法://设置

    - (void)setValue:(NSString *)name age:(NSString *)age
    {
        [nameLbl setText:name];
        [ageLbl setText:age];
    }

    修改TableViewController.m,添加引用:

    #import "TableViewCell.h"

     修改- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"FirstAppTableViewCell";
        
        TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        [cell setValue:[data objectAtIndex:[indexPath row]] age:[data objectAtIndex:[indexPath row]]];//直接用数字了
        return cell;
    }


    编译运行,已经显示出2个不同颜色的label。

    求指点~

  • 相关阅读:
    20155328 《网络攻防》 实验一:PC平台逆向破解(5)M
    20155328 《信息安全系统设计基础》 课程总结
    构建之法
    20155327 2017-2018-2《Java程序设计》课程总结
    20155327 实验五 网络编程与安全
    20155327 网络对抗 实验
    20155327 Exp9 Web安全基础
    20155327 EXP8 Web基础
    20155327 实验四 Android程序设计
    20155327 李百乾 Exp7 网络欺诈防范
  • 原文地址:https://www.cnblogs.com/maxfong/p/2487658.html
Copyright © 2011-2022 走看看