美文网首页
ios uitableview的简单使用(一)

ios uitableview的简单使用(一)

作者: 八角罗特斯 | 来源:发表于2018-11-02 20:44 被阅读0次

    1.UITableView的属性控件和数组

    #pragma mark - 表格
    @property (nonatomic,strong)UITableView *tableview;
    #pragma mark - 数据 (NSMutableArray 可变数组;NSArray 数组)
    @property (nonatomic,strong)NSMutableArray *dataArry;
    

    2.在@implementation中实现,或者叫初始化表格个数组对象,代码:

    -(UITableView *)tableview{
        if(_tableview == nil){
            //bounds 和 frame 区别:bounds,指的是空间本身大小,x=0,y=0;frame,x指的是在父控件的位置和大小
            _tableview = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //        [UIScreen mainScreen].bounds;指的是屏幕大小
            _tableview.dataSource = self;//遵循数据源
            _tableview.delegate = self;//遵循协议
        }
        return _tableview;
    }
    -(NSMutableArray *)dataArry{
        if(_dataArry == nil){
            _dataArry = [NSMutableArray array];//初始化数据
        }
        return _dataArry;
    }
    #pragma mark - UITableViewDelegate,UITableViewDataSource
    //表格组数 Sections 组
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    

    3.遵循数据源(UITableViewDelegate)和代理(UITableViewDataSource)方法

    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
    #pragma mark - 表格
    @property (nonatomic,strong)UITableView *tableview;
    #pragma mark - 数据 (NSMutableArray 可变数组;NSArray 数组)
    @property (nonatomic,strong)NSMutableArray *dataArry;
    

    4.实现数据源(UITableViewDelegate)和代理(UITableViewDataSource)方法,代码:

    -(NSMutableArray *)dataArry{
        if(_dataArry == nil){
            _dataArry = [NSMutableArray array];//初始化数据
        }
        return _dataArry;
    }
    #pragma mark - UITableViewDelegate,UITableViewDataSource
    //表格组数 Sections 组
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    //每组返回行数 Rows 行
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return self.dataArry.count;
    }
    //每个单元格的e内容
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
        if (cell == nil){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellID"];
            
            
        }
        model *m =self.dataArry[indexPath.row];//取出数据元素
        cell.textLabel.text = [NSString stringWithFormat:@"姓名:%@,年龄:%@",m.name,m.age];
        return cell;
    }
    

    注意:使用自定义cell时,注册方法在viewdidLoad中添加,并且需要在viewDidLoad中将表格添加到主视图。代码

    - (void)viewDidLoad {
        [super viewDidLoad];
    //    _tableview = [[UITableView alloc]init];
    //    _tableview.frame = self.view.bounds;
        
        //添加数据元素
        model * m0 =[[model alloc]init];
        m0.name = @"张三";
        m0.age = @"16";
        [self.dataArry addObject:m0];
        //    [self.dataArry addObject:m0];
        //[self.dataArry addObject:@"第一组"];
        //[self.dataArry addObject:@"第二组"];
        [self.view addSubview:self.tableview];//添加表格到视图
        
    

    补充,模型的代码如下

    @interface model : NSObject
    @property(nonatomic,copy)NSString *name;//姓名
    @property(nonatomic,copy)NSString *age;//姓名
    

    相关文章

      网友评论

          本文标题:ios uitableview的简单使用(一)

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