您的位置:首页 > 产品设计 > UI/UE

UITableView的简单使用!

2015-11-03 13:19 525 查看
首先,在IOS的开发中表格视图扮演着重要的作用,现在市面上流行的APP大多有这个,下面我们来简单学习一下。

如果是在UIViewController里面写,那么我们可以写一个UITableView的属性;当然第一步还是要写Get的方法,如果你只是需要一个简单的表格视图,那么你完全可以用系统所提供的cell就行,如果是想自己定义表格视图上的内容,那么就需要你自己建立一个cell的类,这我们下面会讲。

方法:设置行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
    if (_twoTableView.superview);
方法:设置高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_twoTableView.superview)
方法:设置cell(这里添加内容)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

方法:单元格点击方法

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

//当然上面的方法需要写协议方法,下面有代码

附上一个最简单的表格视图:
//声明属性

@property(nonatomic,strong)UITableView;

方法:设置行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{

    if (_twoTableView.superview){

           return 10;//这里一般通过数据的多少来定义有多少行

}

方法:设置高度

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

    if (_twoTableView.superview){

    return 100;//每个单元格的高度

}
方法:单元格点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //这里写入单元格的点击方法
}

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

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KUITableViewCellIdentifierTwo];
            cell.textLabel.text = @"输入内容"; 
            cell.imageView.image
= [UIImage imageNamed:@""];//图片名字
            cell.detailTextLabel.text = @"输入内容";//副文本
            return cell;cell.
}
        }

get方法
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 155, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-64-140)
style:UITableViewStylePlain];
        //设置代理
        _tableView.delegate = self;
        //设置数据源
        _tableView.dataSource = self;

        
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];

        
    }
      return _tableView;

}

*tableView;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uitableview