美文网首页
UITableview(表格)怎么写 初学者的问题 看这里、

UITableview(表格)怎么写 初学者的问题 看这里、

作者: JaneEyre3X | 来源:发表于2018-01-18 15:25 被阅读0次

    UITableview(表格)怎么写 初学者的问题 看这里、

    #import "ViewController.h"
    
    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
    // UITableViewDelegate 它用于表现cell的显示和行为  http://blog.csdn.net/swbl492904919/article/details/50903856
    //UITableViewDataSource   它用于表现cell行数 分区等
    {
    //    我们先定义一个 tableview
        UITableView * table;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
        //UITableViewStyleGrouped 这个样式是 tableview 自定义cell个数等 ,UITableViewStylePlain 则是不需要设置行数 就可以显示出来按照要求来。
        
    //    遵守协议
        table.delegate = self;
        table.dataSource =self;
    //写好之后加入视图 (一定要加要加要加)
        [self.view addSubview:table];
        
    }
    //设置cell有多少行
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    {
        return 6;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    {
    //    一定要写重用机制  否则数据太多会造成tableview 复用
        static NSString * cellID =  @"cell";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        }
        return cell;
    }
    

    相关文章

      网友评论

          本文标题:UITableview(表格)怎么写 初学者的问题 看这里、

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