美文网首页
UITableViewCell

UITableViewCell

作者: Hilarylii | 来源:发表于2017-08-10 17:18 被阅读0次
    //
    //  ViewController.m
    //  UI05_TableView
    //
    //  Created by lanou3g on 17/8/10.
    //  Copyright © 2017年 lanou3g. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    <
    UITableViewDelegate,
    UITableViewDataSource
    >
    
    @property (nonatomic, retain) NSArray *dataArray;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.dataArray = @[@"one",@"two",@"three",@"four",@"five"];
        
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor whiteColor];
        //设置行高
        tableView.rowHeight = 200.f;
        //分割线的样式(没有分割线)
    //    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //分割线的颜色
        tableView.separatorColor = [UIColor greenColor];
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:tableView];
        //头视图
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 300)];
        headerView.backgroundColor = [UIColor redColor];
        tableView.tableHeaderView = headerView;
    }
    
    //设置某-section中的行数
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.dataArray.count;
    }
    //设置某一位置的cell
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        //多种cell
        NSString *name = self.dataArray[indexPath.row];
        if ([@"one" isEqualToString:name]) {
            //从重用池取出@"one"格式的cell
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"one"];
            if (nil == cell) {//如果没有取出来,就重新创建
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"one"];
                //创建对象
                //        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 50, 50)];
                //        view.backgroundColor = [UIColor redColor];
                //        [cell addSubview:view];
                
            }
            cell.textLabel.text = name;
            return cell;
        }
        
        //更新数据
    //    cell.textLabel.text = [NSString stringWithFormat:@"当前行:%ld",indexPath.row];
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"two"];
        if (nil == cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"two"];
            
        }
        cell.textLabel.text = name;
        return cell;
    }
    
    //分区(section)的个数
    //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //    return 10;
    //}
    
    //点击某个cell的时候会触发的方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSLog(@"section:%ld,row:%ld,content:%@",indexPath.section,indexPath.row,self.dataArray[indexPath.row]);
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:UITableViewCell

          本文链接:https://www.haomeiwen.com/subject/wvohrxtx.html