简单tableView的使用

来源:互联网 发布:手机怎么发送淘宝链接 编辑:程序博客网 时间:2024/05/01 12:01

UITableView是一个用于显示列表的视图,可以作为子视图镶嵌在主视图上,可以滑动,选取各种参数

定义:

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{


@property (nonatomic, retain) NSArray *dataList;

@property (nonatomic, retain) UITableView *myTableView;


    UITableView *tableView = [[UITableViewalloc] initWithFrame:FRAME_TABLEVIEWstyle:UITableViewStylePlain];

注意:这里的两个属性 dataList和myTableView并不要求实现,只需要在头文件定义就可以了

Delegate必须要有,因为必须要实现它的接口

使用:

    //init tableView

    NSArray *list = [NSArray arrayWithObjects:@"模糊",@"素描",@"怀旧", nil];

    self.dataList = list;

    // 设置tableView的数据源

    tableView.dataSource = self;

    // 设置tableView的委托

    tableView.delegate = self;

    // 设置tableView的背景图

//    tableView.backgroundView = [[UIImageView alloc] init];

    self.myTableView = tableView;

    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.view addSubview:self.myTableView];

常用的属性设置:

    //设置转置,使table view倒过来

    tableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);

    //取消滑动出现删除

    tableView.showsVerticalScrollIndicator = NO;

接口的实现:

 

//set cells

//定义每个列表项的属性

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

{

    static NSString *CellWithIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellWithIdentifier];

    }

    cell.imageView.image = [UIImageimageNamed:@"example.png"];

    return cell;

}

 

//set number of sections

//定义块的个数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

 

//行首缩进

//定义首行缩进的宽度

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 0;

}

 

//row height

//定义每一行的高度

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

{

    return 105;

}

 

 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

//    if ([indexPath row] % 2 == 0) {

//        cell.backgroundColor = [UIColor blueColor];

//    } else {

//        cell.backgroundColor = [UIColor greenColor];

//    }

}

 

 

//select rows callback

//选择某行以后的动作方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    switch ([indexPath row]) {

        case 0:

            testFilter = [[GPUImageFastBlurFilteralloc] init];

            break;

        case 1:

            testFilter = [[GPUImageSketchFilteralloc] init];

            break;

        case 2:

            testFilter = [[GPUImageSepiaFilteralloc] init];

            break;

        default:

            break;

    } 

}

 

 

// delete

//选择删除以后的方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"执行删除操作");

}

 

//设置没有分界线

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    returnUITableViewCellEditingStyleNone;

}

 

 

//number of rows

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

{

    returnself.dataList.count;

}

 

原创粉丝点击