zoukankan      html  css  js  c++  java
  • 源码-0203-06-自定义非等高cell-xib

    //
    //  XMGStatusesViewController.m
    //  备课03-不等高的cell-非代码
    #import "XMGStatusesViewController.h"
    #import "XMGStatus.h"
    #import "XMGStatusCell.h"
    
    @interface XMGStatusesViewController ()
    @property (strong, nonatomic) NSArray *statuses;
    @end
    
    @implementation XMGStatusesViewController
    
    - (NSArray *)statuses
    {
        if (_statuses == nil) {
            // 加载plist中的字典数组
            NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
            NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
            
            // 字典数组 -> 模型数组
            NSMutableArray *statusArray = [NSMutableArray array];
            for (NSDictionary *dict in dictArray) {
                XMGStatus *status = [XMGStatus statusWithDict:dict];
                [statusArray addObject:status];
            }
            
            _statuses = statusArray;
        }
        return _statuses;
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    }
    
    #pragma mark - Table view data source
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.statuses.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        XMGStatusCell *cell = [XMGStatusCell cellWithTableView:tableView];
        cell.status = self.statuses[indexPath.row];
        return cell;
    }
    
    #pragma mark - 代理方法
    /**
     *  返回每一行的高度
     */
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 250;
    }
    
    
    @end
    //
    //  XMGStatus.h
    //  备课03-不等高的cell-非代码
    #import <Foundation/Foundation.h>
    
    @interface XMGStatus : NSObject
    @property (strong, nonatomic) NSString *name;
    @property (strong, nonatomic) NSString *text;
    @property (strong, nonatomic) NSString *icon;
    @property (strong, nonatomic) NSString *picture;
    @property (assign, nonatomic, getter=isVip) BOOL vip;
    
    + (instancetype)statusWithDict:(NSDictionary *)dict;
    @end
    //
    //  XMGStatus.m
    //  备课03-不等高的cell-非代码
    #import "XMGStatus.h"
    
    @implementation XMGStatus
    + (instancetype)statusWithDict:(NSDictionary *)dict
    {
        XMGStatus *status = [[self alloc] init];
        [status setValuesForKeysWithDictionary:dict];
        return status;
    }
    @end
    //
    //  XMGStatusCell.h
    //  07-自定义非等高cell-xib
    #import <UIKit/UIKit.h>
    @class XMGStatus;
    
    @interface XMGStatusCell : UITableViewCell
    + (instancetype)cellWithTableView:(UITableView *)tableView;
    
    /** 微博模型数据 */
    @property (nonatomic, strong) XMGStatus *status;
    @end
    //
    //  XMGStatusCell.m
    //  07-自定义非等高cell-xib
    #import "XMGStatusCell.h"
    #import "XMGStatus.h"
    
    @interface XMGStatusCell()
    @property (weak, nonatomic) IBOutlet UIImageView *iconView;
    @property (weak, nonatomic) IBOutlet UILabel *nameLabel;
    @property (weak, nonatomic) IBOutlet UIImageView *vipView;
    @property (weak, nonatomic) IBOutlet UILabel *contentLabel;
    @property (weak, nonatomic) IBOutlet UIImageView *pictureView;
    
    @end
    
    @implementation XMGStatusCell
    
    + (instancetype)cellWithTableView:(UITableView *)tableView
    {
        static NSString *ID = @"status";
        XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
        }
        return cell;
    }
    
    - (void)setStatus:(XMGStatus *)status
    {
        _status = status;
        
        if (status.isVip) {
            self.nameLabel.textColor = [UIColor orangeColor];
            self.vipView.hidden = NO;
        } else {
            self.nameLabel.textColor = [UIColor blackColor];
            self.vipView.hidden = YES;
        }
        
        self.nameLabel.text = status.name;
        self.iconView.image = [UIImage imageNamed:status.icon];
        if (status.picture) {
            self.pictureView.hidden = NO;
            self.pictureView.image = [UIImage imageNamed:status.picture];
        } else {
            self.pictureView.hidden = YES;
        }
        self.contentLabel.text = status.text;
    }
    
    @end
    本人无商业用途,仅仅是学习做个笔记,特别鸣谢小马哥,学习了IOS,另日语学习内容有需要文本和音频请关注公众号:riyuxuexishuji
  • 相关阅读:
    Mac Mysql 修改初始化密码
    网址收藏
    Xcode 模拟器复制解决方案
    ios优秀的第三方框架
    CocoaPods第三方库管理工具
    ios网络请求
    java面试宝典
    SQL分表
    FastDFS+Nginx+Module
    分布式文件系统FastDFS架构认知
  • 原文地址:https://www.cnblogs.com/laugh/p/6438567.html
Copyright © 2011-2022 走看看