Ios UITextField文本在自定义单元格中重复
所以我有这个密码Ios UITextField文本在自定义单元格中重复,ios,objective-c,uitableview,Ios,Objective C,Uitableview,所以我有这个密码 if (tableView.tag == RECO_TABLE) { CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"]; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; NSString *roomName = [selecte
if (tableView.tag == RECO_TABLE) {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
NSString *roomName = [selectedRooms objectAtIndex:indexPath.row];
[cell.cellRoomName setText:roomName];
return cell;
}
在我的定制牢房里是
@property (weak, nonatomic) IBOutlet UILabel *cellRoomName;
@property (weak, nonatomic) IBOutlet UITextField *cellRoomPrice;
cellRoomName很好,但当我的行数达到>5时,cellRoomPrice会不断重复,我知道重用单元的概念,但我不知道如何开始
顺便说一句,这是我的示例场景
第1行=40
第2行=2
第3行=空
第4行=空
第5行=空
第6行=40
第7行=2
因此,每第5行我的行就会被复制。您到达的行>5会被复制,因为我没有为它设置值!TableView在单元格之前重用,所以如果不为其设置新值,TableView将在单元格之前使用 看起来您没有正确设置
cellRoomPrice
的值。如果已经为每行设置了此值,则需要为每一单元格/行设置此值
因为您允许用户编辑房间价格,所以您需要将该值存储在某个位置,最好是存储在按行号索引的数组中。假设您有一个UITextFieldDelegate
,您还需要知道用户正在-textfieldEndediting:
上编辑的行号(以存储值),这可以通过在-cellForRowAtIndexPath:
上设置标记来解决
希望这有帮助。尝试一下,因为您没有检查单元格是否已分配
if (tableView.tag == RECO_TABLE) {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if(!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
NSString *roomName = [selectedRooms objectAtIndex:indexPath.row];
[cell.cellRoomName setText:roomName];
return cell;
}
在检查单元格是否为nil之前,您需要对代码进行一些更改
if (tableView.tag == RECO_TABLE) {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if(!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
NSString *roomName = [selectedRooms objectAtIndex:indexPath.row];
[cell.cellRoomName setText:roomName];
return cell;
}
然后,您在单元格文本字段中键入的文本也需要存储在其他数组中,比如“selectedRoomsPrices”和“cellForRowAtIndexPath:”中,您需要检查计数并在“cell.CellRoomPrices”中设置该值,如下所示:
if (tableView.tag == RECO_TABLE) {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if(!cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
NSString *roomName = [selectedRooms objectAtIndex:indexPath.row];
[cell.cellRoomName setText:roomName];
NSString *roomPrice = @"";
if(selectedPrices.count > indexPath.row) {
roomPrice = [selectedRoomsPrices objectAtIndex:indexPath.row];
}
[cell.cellRoomPrice setText:roomPrice];
return cell;
}
我看不出这有什么奇怪的。为什么你说第五排是重复的。谢谢你的想法。。。我已将NSDictionary用作数据对象,并将其保存在NSUSerDefault中,以便将其检索回来:)