遇到的需求如下
需求需要对住宿类型进行添加,有多个需要商户输入的元素,比如价格,时间,房间类型,图片......
在这个情况下肯定选用tableView来实现,但是tableViewCell的复用会导致数据的错乱,如果不复用又会内存消耗比较大,如果找到复用cell且数据不错乱的方法就成了最优选择。
这个问题其实只需要找到tableView的代理方法就行了。
//cell移出屏幕显示是回调的代理
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
在这个代理里面更新cell的数据到对应的model中即可。
ViewController.m代码如下
#import "ViewController.h"
#import "InputTableViewCell.h"
#import "RoomModel.h"
static NSString *identifier = @"cell";
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *mainTB;
@property (nonatomic, strong) NSMutableArray *rooms;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.mainTB.rowHeight = 200;
self.rooms = [[NSMutableArray alloc]init];
for (NSInteger i = 0; i < 30; i++) {
RoomModel *model = [[RoomModel alloc]init];
model.name = @"";
model.price = @"";
[self.rooms addObject:model];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 30;
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
RoomModel *model = self.rooms[indexPath.row];
InputTableViewCell *inputCell = (InputTableViewCell *)cell;
model.name = inputCell.nameTF.text;
model.price = inputCell.priceTF.text;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
InputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[InputTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.indexLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
RoomModel *model = self.rooms[indexPath.row];
cell.nameTF.text = model.name;
cell.priceTF.text = model.price;
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
RoomModel类代码如下
//RoomModel.h
#import <Foundation/Foundation.h>
@interface RoomModel : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSString *price;
@end
//RoomModel.m
#import "RoomModel.h"
@implementation RoomModel
@end
InputTableViewCell类代码如下
//InputTableViewCell.h
#import <UIKit/UIKit.h>
@interface InputTableViewCell : UITableViewCell
@property (nonatomic, strong) UITextField *nameTF;
@property (nonatomic, strong) UITextField *priceTF;
@property (nonatomic, strong) UILabel *indexLabel;
@end
//InputTableViewCell.m
#import "InputTableViewCell.h"
#import "Masonry.h"
@implementation InputTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.indexLabel = [[UILabel alloc]init];
[self.contentView addSubview:self.indexLabel];
self.nameTF = [[UITextField alloc]init];
self.nameTF.borderStyle = UITextBorderStyleRoundedRect;
[self.contentView addSubview:self.nameTF];
self.priceTF = [[UITextField alloc]init];
self.priceTF.borderStyle = UITextBorderStyleRoundedRect;
[self.contentView addSubview:self.priceTF];
[self.indexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.equalTo(self.contentView).offset(0);
make.width.equalTo(@30);
}];
[self.nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.indexLabel.mas_right).offset(8);
make.top.equalTo(self.contentView).offset(8);
make.right.equalTo(self.contentView).offset(-8);
make.bottom.equalTo(self.priceTF.mas_top).offset(-8);
make.height.equalTo(self.priceTF.mas_height);
}];
[self.priceTF mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.indexLabel.mas_right).offset(8);
make.right.bottom.equalTo(self.contentView).offset(-8);
}];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
这样就通过复用TableViewCell来实现数据的管理,多看一下TableView的方法,也许问题就很简单了。