当前位置: 首页 > 工具软件 > tableview > 使用案例 >

简单的tableView

白博易
2023-12-01
//
//  ViewController.m
//  tableViewApp1
//
//  Created by apple  on 14-8-16.
//  Copyright (c) 2014年 xq. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *myTableView;
    
}

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    
    myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    [myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    myTableView.delegate = self;
    myTableView.dataSource = self;
//    myTableView.tag = 1; 如果在一个页面显示多个tableView则需要打好标签.好区分。
    
    //设置tableView的headerView
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];//这里的坐标只有height设置有用。
    headerView.backgroundColor = [UIColor cyanColor];
    myTableView.tableHeaderView = headerView;
    
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 40)];
//    label.text =@"tableHeaderView";
//    //tableHeaderView的类型为UIView * ,因此只要是它的子类都可以显示上去。
//    myTableView.tableHeaderView = label;
    
    //设置tableView的footerView
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];//这里的坐标中只有x和height改变有用。
    footerView.backgroundColor = [UIColor purpleColor];
    myTableView.tableFooterView = footerView;
    
    [self.view addSubview:myTableView];
}


//为每个section设置行数。(UITableView *)tableView 这个参数可能是(tableView.tag)为区分是哪个tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    switch (section) {
//        case 0:
//            return 3;
//            break;
//        case 1:
//            return 4;
//            break;
//        case 2:
//            return 5;
//            break;
//        default:
//            return 0;
//            break;
//    }
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //到tableView中获取空闲的cell,如果没有就返回nil
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    //设置每个section的内容
//    if (indexPath.section == 0) {
//        cell.textLabel.text = @"ssdsd";
//    }
//    else if(indexPath.section == 1)
//    cell.textLabel.text = @"Zealer和老罗陷入口水的时候,另一家视频评测网站FView却要为生存做出选择";
//    else cell.textLabel.text = @"11111";
    
    cell.textLabel.text = @"Zealer和老罗陷入口水的时候,另一家视频评测网站FView却要为生存做出选择";
    
    return cell;
}

返回tableView中有几个section,默认为1.
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 3;
//}


//设置每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

//设置section的header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

设置section的footer高度后就不会出现多出行的cell *****
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return 50;
//}


为section的header添加视图
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *view = [[UIView alloc]init];
//    view.backgroundColor = [UIColor redColor];
//    return view;
//}

为section的footer添加视图,由于返回值类型为UIView,所以只要是UIView及其子类的对象都可以返回。
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor yellowColor];
//    
//    UIImage *image = [UIImage imageNamed:@"1.jpg"];
//    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    imageView.alpha = 0;
//    
    UILabel *label = [[UILabel alloc]init];
    label.text =@"sdad";
//    
//
//    return imageView;
//}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *str = @"titleForHeader";
    return  str;
}

上面的viewForFooterInSection显示优先级>titleForFooterInSection,因此只要有viewForFooter它就会覆盖titleForFooter,即使viewForFooter的透明度为0。
//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
//{
//    NSString *str = @"titleForFooter";
//    return str;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 类似资料: