美文网首页
XIB自定义不等高cell(ios8之后)

XIB自定义不等高cell(ios8之后)

作者: 陆号 | 来源:发表于2017-08-02 17:02 被阅读18次
    cell样式.png

    1.cell

    @interface QPStatusCell ()
    
    /** 图像 */
    @property (nonatomic, weak)IBOutlet UIImageView *iconImageView;
    /** 昵称 */
    @property (nonatomic, weak)IBOutlet UILabel *nameLabel;
    /** vip */
    @property (nonatomic, weak)IBOutlet UIImageView *vipImageView;
    /** 正文 */
    @property (nonatomic, weak)IBOutlet UILabel *text_Label;
    /** 配图 */
    @property (nonatomic, weak)IBOutlet UIImageView *pictureImageView;
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *pitureHeight;
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureBottom;
    
    @end
    
    @implementation QPStatusCell
    
    
    /**
     *  设置子控件的数据
     */
    - (void)setStatus:(XMGStatus *)status
    {
        _status = status;
        self.iconImageView.image = [UIImage imageNamed:status.icon];
        self.nameLabel.text = status.name;
        
        if (status.isVip) {
            self.nameLabel.textColor = [UIColor orangeColor];
            self.vipImageView.hidden = NO;
        } else {
            self.vipImageView.hidden = YES;
            self.nameLabel.textColor = [UIColor blackColor];
        }
        
        self.text_Label.text = status.text;
        
        if (status.picture) { // 有配图
            self.pictureImageView.hidden = NO;
            self.pictureImageView.image = [UIImage imageNamed:status.picture];
            self.pitureHeight.constant = 100;
            self.pictureBottom.constant = 10;
        } else { // 无配图
            self.pictureImageView.hidden = YES;
            self.pitureHeight.constant = 0;
            self.pictureBottom.constant = 0;
        }
    }
    
    @end
    
    

    2.UITableViewController

    @interface ViewController ()
    
    /** 所有的微博数据 */
    @property (nonatomic, strong) NSArray *statuses;
    @end
    
    @implementation ViewController
    
    - (NSArray *)statuses
    {
       if (!_statuses) {
           _statuses = [QPStatus mj_objectArrayWithFilename:@"statuses.plist"];
       }
       return _statuses;
    }
    
    
    - (void)viewDidLoad {
       [super viewDidLoad];
       
       // self-sizing(iOS8 以后)
       // 告诉tableView所有cell的真实高度是自动计算的(根据设置的约束)
       self.tableView.rowHeight = UITableViewAutomaticDimension;
       // 设置估算高度
       self.tableView.estimatedRowHeight = 44;
    }
    
    #pragma mark - 数据源方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
       return self.statuses.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       static NSString *ID = @"status";
       // 访问缓存池
       QPStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
       
       // 传递模型数据
       cell.status = self.statuses[indexPath.row];
       return cell;
    }
    
    
    @end
    
    

    相关文章

      网友评论

          本文标题:XIB自定义不等高cell(ios8之后)

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