现在的位置: 首页 > 综合 > 正文

UITableView,UIScrollView,UITableViewCell

2014年08月29日 ⁄ 综合 ⁄ 共 1912字 ⁄ 字号 评论关闭

UITableView继承于UIScrollView,每一个单元格是由UITableViewCell表示的。

创建UITableView

 UITableView *aTable=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) style: UITableViewStylePlain];

其中Style是用来设置表视图的样式:UITableViewStylePlain//简单样式 UITableStyleGrouped//分组样式

UITableView重要属性

separatoColor:表视图Cell的样式

tableHeaderView:表头的View

tableFooterView:表尾的View

UITableViewCell系统自带的四个属性,一般为了做出更多种多样的效果,我选择自定义一个CELL

NewCell.h

@property(nonatomic,retain)  UILabel *name;
@property(nonatomic,retain) UIImageView *headImage;
@property(nonatomic,retain)UILabel *time;
@property(nonatomic,retain)UILabel *times;

NewCell.m

-(void)dealloc{
    [_headImage release];
    [_name release];
    [_time release];
    [_times release];
    [super dealloc];
}

self.name=[[UILabel alloc] initWithFrame:CGRectZero];
        
        [self.contentView addSubview:_name];
        [_name release];
        
        self.headImage=[[UIImageView alloc] initWithFrame:CGRectZero];
        
        _headImage.layer.cornerRadius=35.0;
        _headImage.layer.masksToBounds=YES;
        [self.contentView addSubview:_headImage];
        [_headImage release];
        
        
        
        self.time=[[UILabel alloc] initWithFrame:CGRectZero];
        
        [self.contentView addSubview:_time];
        [_time release];

        self.times=[[UILabel alloc] initWithFrame:CGRectZero];

        [self.contentView addSubview:_times];
        [_times release];

为了代码规范Label的尺寸设定一般写一个方法

-(void)layoutSubviews{
    [_name setFrame:CGRectMake(90, 15, 150, 40)];
    [_headImage setFrame:CGRectMake(10, 15, 70, 70)];
    [_time setFrame:CGRectMake(90, 55, 150, 20)];
    [_times setFrame:CGRectMake(280, 60, 40, 15)];
}

使用UITableView的时候需要引入一个协议UITableViewDataSource,里面有两个必须实现的方法

设定Cell的数量

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
设定重用池
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

设定重用池的名字
static NSString * cellIdentify =@"cellIdentify";
    
    //指向重用池
    NewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentify];
    //如果不为空
    if (cell ==Nil)
    {
        cell=[[[NewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify] autorelease];
        
    }

【上篇】
【下篇】

抱歉!评论已关闭.