方法/步骤
1、 Province.h
#import
@interface Province : NSObject
// UI控件用weak,NSString用copy,其他对象一般用strong
@property (nonatomic, copy) NSString *header;
@property (nonatomic, copy) NSString *footer;
@property (nonatomic, strong) NSArray *citites;
+ (id)provinceWithHeader:(NSString *)header footer:(NSString *)footer cities:(NSArray *)cities;
@end
2、Province.m
#import "Province.h"
@implementation Province
+ (id)provinceWithHeader:(NSString *)header footer:(NSString *)footer cities:(NSArray *)cities
{
Province *p = [[Province alloc] init];
p.header = header;
p.footer = footer;
p.citites = cities;
return p;
}
@end
3、MJViewController.h
#import
@interface MJViewController : UIViewController
@end
4、MJViewController.m
#import "MJViewController.h"
@interface MJViewController ()
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark 一共1组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma mark 这一组里面有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 9;
}
#pragma mark 返回第indexPath这行对应的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
Default : 不显示detailTextLabel
Value1 : 在右边显示detailTextLabel
Value2 : 不显示图片,显示detailTextLabel
Subtitle : 在底部显示detailTextLabel
*/
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
cell.textLabel.text = [NSString stringWithFormat:@"产品-%d", indexPath.row];
// 设置详情文字
cell.detailTextLabel.text = [NSString stringWithFormat:@"产品-%d非常好!!!!!", indexPath.row];
// 设置图片
NSString *imgName = [NSString stringWithFormat:@"00%d.png", indexPath.row + 1];
cell.imageView.image = [UIImage imageNamed:imgName];
// 设置最右边的小图标
// if (indexPath.row % 2 == 0) {
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// } else {
// cell.accessoryType = UITableViewCellAccessoryNone;
// }
// 设置最右边的显示什么控件
cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
return cell;
}
#pragma mark - 代理方法
#pragma mark 返回indexPath这行cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// return 70 + indexPath.row * 20;
return 70;
}
@end
UIView的封装
如果一个view内部的子控件比较多,一般会考虑自定义一个view,把它内部子控件的创建屏蔽起来,不让外界关心
外界可以传入对应的模型数据给view,view拿到模型数据后给内部的子控件设置对应的数据
继承自系统自带的控件,写一个属于自己的控件
目的:封装空间内部的细节,不让外界关心
类似于下面这张图,如果把下面所有控件加入到控制器的View中,控制器的View将拥有太多子控件,而且非常不好管理每个控件的位置,这个时候把他们封装起来,外界不用关心其内部结构,那将方便太多。