遇到的需求如下
需求需要對(duì)住宿類型進(jìn)行添加覆糟,有多個(gè)需要商戶輸入的元素,比如價(jià)格馋记,時(shí)間,房間類型懊烤,圖片......
在這個(gè)情況下肯定選用tableView來(lái)實(shí)現(xiàn)梯醒,但是tableViewCell的復(fù)用會(huì)導(dǎo)致數(shù)據(jù)的錯(cuò)亂,如果不復(fù)用又會(huì)內(nèi)存消耗比較大奸晴,如果找到復(fù)用cell且數(shù)據(jù)不錯(cuò)亂的方法就成了最優(yōu)選擇。
這個(gè)問(wèn)題其實(shí)只需要找到tableView的代理方法就行了日麸。
//cell移出屏幕顯示是回調(diào)的代理
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
在這個(gè)代理里面更新cell的數(shù)據(jù)到對(duì)應(yīng)的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
這樣就通過(guò)復(fù)用TableViewCell來(lái)實(shí)現(xiàn)數(shù)據(jù)的管理,多看一下TableView的方法代箭,也許問(wèn)題就很簡(jiǎn)單了墩划。