美文网首页
IOS入门之UITableView

IOS入门之UITableView

作者: 爆发小宇 | 来源:发表于2017-02-04 15:25 被阅读42次

    今天入门了UITableView,跟Android的ListView很类似

    直接上代码了

    在.h文件中要实现两个接口,分别是UITableViewDataSource,UITableViewDelegate

    以下代码是在.m文件中

    static NSString *identifier = @"LinkerCell";
    
    // 初始化tableview
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
      // 注册nib文件
        [self.tableView registerNib:[UINib nibWithNibName:@"LinkerCell" bundle:nil] forCellReuseIdentifier:identifier];
    }
    

    然后实现三个方法

    // 返回data中的数量
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        if (mData) {
            return mData.count;
        }
        return 0;
    }
    
    // 返回cell
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        LinkerCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        [cell setLinker:[mData objectAtIndex:indexPath.row]];
        return cell;
    }
    
    
    // 返回cell的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 90.0f;
    }
    

    相关文章

      网友评论

          本文标题:IOS入门之UITableView

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