zoukankan      html  css  js  c++  java
  • 【转】iOS 通过xib自定义UITableViewCell【原创】

    原文网址:http://blog.it985.com/9683.html

    在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观。并且要不停的调整位置,字体什么的。这时,我们可以通过在tableViewCell的xib上搭建会更加直观,有效提高开发效率。
    首先,在我们创建了工程之后,新建XIB的cell。command+n,选择Cocoa Touch Class
    屏幕快照 2015-04-07 下午10.54.49
    然后选择UITableViewCell类型,同时钩上Also Create xib File
    屏幕快照 2015-04-07 下午10.55.30
    之后,在对应的cell的xib上搭建我们需要的样式
    屏幕快照 2015-04-07 下午11.03.00
    再在tableView中配合对应的代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIndentifier = @"MyTableViewCell";//这里的cellID就是cell的xib对应的名称
        MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIndentifier];
        if(nil == cell) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIndentifier owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
         
        _tableView.rowHeight = cell.frame.size.height;//注意,这里我们要把table的rowHeight设为和cell的高度一样
        return cell;
    }

    之后我们来看下运行效果
    屏幕快照 2015-04-07 下午11.03.40

    最后,奉上demo
    通过xib自定义UITableViewCell

  • 相关阅读:
    音律入门
    [转]MIDI常识20条
    Java使用代理服务器
    java8日期时间
    误删课表系统
    Uncaught Error: Bootstrap tooltips require Tether (http://github.hubspot.com/tether/)
    SpringBoot应用部署[转]
    如何学习新技术
    Maven使用archetype迅速生成项目骨架
    两个月打工总结
  • 原文地址:https://www.cnblogs.com/wi100sh/p/5608815.html
Copyright © 2011-2022 走看看