遇到的需求如下
需求需要對住宿類型進行添加,有多個需要商戶輸入的元素,比如價格,時間,房間類型,圖片......
在這個情況下肯定選用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的方法,也許問題就很簡單了。