UITableView的使用

来源:互联网 发布:文化部网络歌曲黑名单 编辑:程序博客网 时间:2024/06/05 21:52

1.简单的UITableView创建:

//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];

//    view.backgroundColor =[UIColor purpleColor];

   

    self.navigationController.navigationBar.translucent = NO;

    self.taVi = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-64)style:UITableViewStylePlain];//屏幕适配大小   tableView样式

    _taVi.delegate = self;

    _taVi.dataSource = self;

    //属性

    _taVi.rowHeight = 50;//行高

    _taVi.separatorColor = [UIColor brownColor];//分割线颜色

//    _taVi.tableHeaderView = view;//table上方可添加UIview类的任意视图

    _taVi.backgroundColor = [UIColor grayColor];

    

    [self.view addSubview:_taVi];

    [_taVi release];

//    [view release];

    



,2在使用UITableView控件的时候,必须遵循两个协议:

#import <UIKit/UIKit.h>


@interface MainViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{

    NSMutableArray * _tableArray;

}

@property(nonatomic,retain)UITableView *taVi;

@end


3,遵循协议的时候,有两个必须要实现的方法:


//行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    NSLog(@"%d",1);

//    if (section == 0) {

//        return 3;

//    }else{

//        return 4;

//    }

   // return [_tableArray count];

     return 10;//行数


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    static NSString*cellIndentify =@"cell";//重用池命名

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentify];//根据面重命名的key值池子里取cell

    if (!cell) {

        cell = [[[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentify]autorelease];//创建一个cell把它放在以@"cell"命名的重用池里面;

    }

    NSLog(@"section====%d,row=====%d",indexPath.section,indexPath.row);

    NSString * str= [_tableArray objectAtIndex:indexPath.row];

     NSLog(@"%d",2);

//    [cell.textLabel setText:str];//设置内容

//    cell.imageView.image =[UIImage imageNamed:@"1.jpg"];//颜色

//    [cell.detailTextLabel setText:@"15842651312"];//

    cell.titleLabel.text = str;

    cell.detailLabel.text = @"12345678";

    cell.timeLabel.text = @"8:16";

    cell.image.image = [UIImage imageNamed:@"1.jpg"];

    return cell;

    

    

    

//   设置CELL的样式

    

    //        cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    //灰色

    //    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    //无颜色

    //        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    //向右箭头样式

    //        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //向右箭头button

    //  cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

}


4.其他非必须实现的协议方法:

//分区

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    // Return the number of sections.

    return 2;

}

//设置区域的头名称

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (section == 0) {

        return @"A";

    }

    return @"B";

}

//设置区域的脚名称

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

   return @"B";

}

//是否允许行移动

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    

    return YES;

}

//行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

//    if (indexPath.section ==0) {

//        if (indexPath.row ==0) {

//            return 40;

//        }else if(indexPath.row == 1)

//            return 80;

//    }else if(indexPath.row==2)

//        return 100;

    return 40;

}

//区域的头名称区域高

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 20;

}

//区域的脚名称区域高

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 20;

}

//设置UITableView的右侧索引

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    

    NSMutableArray *arr = [NSMutableArray array];

    for (int i = 65; i <= 90; i++) {

        [arr addObject:[NSString stringWithFormat:@"%c", i]];

    }

    return arr;


    

}

上述的TableCell时系统自带的,当然了,我们也可以自定义:

创建新的类,继承与UITableCell:

.h文件中,创建四个属性:

@interface CustomTableViewCell : UITableViewCell

@property(nonatomic,retain)UIImageView *image;

@property(nonatomic,retain)UILabel *titleLabel;

@property(nonatomic,retain)UILabel *detailLabel;

@property(nonatomic,retain)UILabel *timeLabel;

@end


在.m中:

#import "CustomTableViewCell.h"


@implementation CustomTableViewCell

- (void)dealloc

{

    [_image release];

    [_titleLabel release];

    [_detailLabel release];

    [_timeLabel release];

    _image= nil;

    _titleLabel = nil;

    _detailLabel= nil;

    _timeLabel=nil;

    

    [super dealloc];

}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        // Initialization code

        self.image = [[UIImageView alloc]initWithFrame:CGRectZero];

        [self.contentView addSubview:_image];

        [_image release];

        

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

        [self.contentView addSubview:_titleLabel];

        [_titleLabel release];

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

        [self.contentView addSubview:_detailLabel];

        [_detailLabel release];

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

        [self.contentView addSubview:_timeLabel];

        [_timeLabel release];

        

        

    }

    return self;

}

//第一种,当前视图添加到父视图

//第二种,当前视图改变坐标时

-(void)layoutSubviews{

    [super  layoutSubviews];

//    对已经创建的子视图重新赋值

    [self.image setFrame:CGRectMake(10, 5, 35, 35)];

    [self.titleLabel setFrame:CGRectMake(80, 3, 200, 30)];

    [self.detailLabel setFrame:CGRectMake(80, 25, 200, 20)];

    [self.timeLabel setFrame:CGRectMake(200, 5, 50, 30)];

}


- (void)awakeFromNib

{

    // Initialization code

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

    [super setSelected:selected animated:animated];


    // Configure the view for the selected state

}









}





0 0
原创粉丝点击