iOS UITableView表视图(1)

    



//在.h文件中声明一下

    //例如:@property(nonatomic,strong)UITableView *table;

    //创建一个UITableView

    self.table = [[UITableView alloc] initWithFrame:self.bounds style:(UITableViewStylePlain)];

    //设置行的高度

    self.table.rowHeight = 260.0;

    //设置分割线的颜色

    self.table.separatorColor = [UIColor redColor];

    //设置分割线的格式

    self.table.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;

    [self addSubview:self.table];

/*---------------------UITableViewDataSource的方法-------------------------*/

//设置在分区中有几行

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

{

    

    return 5;

}

//设置cell(cell 就是UITableView中的每一个单元格)

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

{

    //创建cell

    /*UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:nil];

    cell.textLabel.text = @"今天很开心";

    cell.detailTextLabel.text = @"我中了500W";

    cell.imageView.image = [UIImage imageNamed:@"DSC_8898.JPG"];*/

    //考虑到如果数据多了,是不是得创建好多cell,所以咱们这里用了重用机制

    //UITableViewCell是靠MutableSet来实现重用机制

    //创建一个标识

    static NSString *cell_id = @"a";

    //创建cell

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];

    //判断cell在MutableSet池中如果没有就重新创建

    if (nil == cell)

    {

        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cell_id];

        

    }

    //设置标题

    cell.textLabel.text = @"今天很开心";

    //设置副标题

    cell.detailTextLabel.text = @"可以回家了弟弟";

    //

    return cell;

    

}

//设置 几个分区

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return self.groupArray.count;

}

//设置头部信息

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

{

    NSArray *a = @[@"李",@"张"];

    

    return a[section];

}

//设置底部信息

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

{

    NSArray *a = @[@"李的版权",@"张的版权"];

    return a[section];

}


/*------------------------UITableViewDelegate的方法---------------------*/

//点击cell的时候触发事件

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

{

    NSLog(@"Hello,World");

    

}

//设置行高(好处就是可以设置每一行的高度)

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

{   //例如:想让第一行的高度改变

    if (indexPath.row == 0)

    {

        return 100;

    }


    return 60.0;

}

//头标题高度

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

{

    return 50;

}

//底部高度

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

{

    return 50;

}

//在头标题里可以设置UIView的子类

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    //例如 设置一个button

    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];

    button.backgroundColor = [UIColor orangeColor];

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];

    return button;

}

//在头标题里可以设置UIView的子类(只要是UIView 的子类都可以在里面设置)

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

{

    //例如 设置一张图片

    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DSC_8898.JPG"]];

    return img;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值