自己工作中使用的, 以后可能还需要使用,现在保存起来, 方便以后使用.(使用Masonry 自动布局)
实现效果
核心代码
.m 文件
#import "ViewController.h"
#import "FindTableViewCell.h"
@interface ViewController () <UITableViewDelegate, UITableViewDataSource> {
UITableView *_tableView;
NSDictionary *_dataDic;
NSArray *_kArray;
}
@end
static NSString *identifier = @"cell";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setUpView];
_kArray = @[@"1", @"2", @"3",@"4"];
_dataDic = @{_kArray[0]: @[@{@"img": @"find_erweima.png", @"text": @"扫一扫"},
@{@"img": @"find_yaoyiyao.png", @"text": @"摇一摇"}],
_kArray[1]: @[@{@"img": @"find_fujin.png", @"text": @"附近的朋友"},
@{@"img": @"find_device.png", @"text": @"附近的设备"},
@{@"img": @"find_fjrenwu.png", @"text": @"附近的任务"}],
_kArray[2]: @[@{@"img": @"find_chuiyichui.png", @"text": @"吹一吹"},
@{@"img": @"find_tingyiting.png", @"text": @"听一听"}],
_kArray[3]: @[@{@"img": @"find_kanyikan4.png", @"text": @"看一看"},
@{@"img": @"find_guangyiguang.png", @"text": @"逛一逛"},
@{@"img": @"find_kanyikan3.png", @"text" : @"侃一侃"}]
};
}
- (void)setUpView {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 ) style:UITableViewStyleGrouped];
_tableView.rowHeight = 44;
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
[self.view addSubview:_tableView];
[_tableView registerClass:[FindTableViewCell class] forCellReuseIdentifier:identifier]; // 注册cell
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//NSLog(@"count = %ld",_kArray.count);
return _kArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *key = _kArray[section]; // 为了得到key
NSArray *array = _dataDic[key]; // 通过key 找到value
NSLog(@"section = %ld ,cell.count = %ld",section,array.count);
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = _kArray[indexPath.section];
NSArray *array = _dataDic[key]; //
// NSLog(@"count = %lu", (unsigned long)array.count);
FindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
NSDictionary *info = array[indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.infoDataDic = info;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 20;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
FindTableViewCell.h 文件
#import <UIKit/UIKit.h>
@interface FindTableViewCell : UITableViewCell
@property (nonatomic, strong) NSDictionary *infoDataDic;
@end
FindTableViewCell.m 文件
#import "FindTableViewCell.h"
#import "Masonry.h"
@interface FindTableViewCell()
@property (nonatomic, strong) UIImageView *picImage;
@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation FindTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self setUpView];
}
return self;
}
- (void)setUpView
{
self.picImage = [[UIImageView alloc] init];
[self.contentView addSubview:_picImage];
__weak typeof (self) temp = self;
[_picImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(temp.contentView).with.offset(20);
make.centerY.equalTo(temp.contentView.mas_centerY);
make.height.mas_equalTo(@25);
make.width.mas_equalTo(@25);
}];
self.titleLabel = [[UILabel alloc] init];
[self.contentView addSubview:_titleLabel];
[_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(temp.picImage.mas_right).with.offset(10);
make.centerY.equalTo(temp.contentView.mas_centerY);
}];
}
- (void)setInfoDataDic:(NSDictionary *)infoDataDic
{
_infoDataDic = infoDataDic;
_picImage.image = [UIImage imageNamed:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", infoDataDic[@"img"]]]];
_titleLabel.text = infoDataDic[@"text"];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end