作者:liuc | 来源:互联网 | 2024-12-09 13:20
// XMGStatusesViewController.m
// 自定义非等高单元格实现
#import "XMGStatusesViewController.h"
#import "XMGStatus.h"
#import "XMGStatusCell.h"
@interface XMGStatusesViewController ()
@property (strong, nonatomic) NSArray *statuses;
@end
@implementation XMGStatusesViewController
- (NSArray *)statuses {
if (!_statuses) {
// 从本地资源加载数据
NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses" ofType:@"plist"];
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 - 表视图数据源
- (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
// 自定义非等高单元格实现
#import
@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
// 自定义非等高单元格实现
#import "XMGStatus.h"
@implementation XMGStatus
+ (instancetype)statusWithDict:(NSDictionary *)dict {
XMGStatus *status = [[self alloc] init];
[status setValuesForKeysWithDictionary:dict];
return status;
}
@end
// XMGStatusCell.h
// 自定义非等高单元格XIB实现
#import
@class XMGStatus;
@interface XMGStatusCell : UITableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@property (nonatomic, strong) XMGStatus *status;
@end
// XMGStatusCell.m
// 自定义非等高单元格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) {
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
本示例代码展示了如何使用XIB文件来自定义非等高的单元格,并在UITableView中动态调整每个单元格的高度。