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