【发布时间】:2019-06-23 08:11:57
【问题描述】:
类似的问题已经被问过很多次,并且提到了很多解决方案,但没有一个对我有用,因为它们解决了固定数量的单元格,而我有动态数量的单元格。问题来了:
它在 Objective-C 中。我有 动态行数的 tableview(用户可以根据需要添加更多单元格或删除单元格) 并且 每行包含 3 个文本字段,最后我想从该文本字段中获取数据并保存它们。从所有文本字段中获取数据都运行良好,但是当我将数据添加到任何行(3 个文本字段)中时,主要问题出现了,当我滚动它时它会重复。
代码如下:
-(void)textFieldDidEndEditing:(UITextField *)textField{
if ([_txtNumbers resignFirstResponder]) {
totalIndex = [_txtNumbers.text integerValue];
[_tblContacts reloadData];
}
}
- (IBAction)onAddCellClicked:(id)sender {
totalIndex = [_txtNumbers.text integerValue];
totalIndex = totalIndex + 1;
_txtNumbers.text = [NSString stringWithFormat:@"%ld",(long)totalIndex];
[_tblContacts reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return totalIndex;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"InviteEmailTableViewCell";
InviteEmailTableViewCell *cell = [self.tblContacts dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.txtName.delegate = self;
cell.txtTell.delegate = self;
cell.txtEmail.delegate = self;
UIToolbar *numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[numberToolbar setTintColor:kShadowColor1];
[numberToolbar setBarTintColor:[UIColor blackColor]];
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad:)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad:)],
nil];
cell.txtTell.inputAccessoryView = numberToolbar;
[cell.txtName setTag:indexPath.row];
[cell.txtTell setTag:indexPath.row];
[cell.txtEmail setTag:indexPath.row];
return cell;
}
接下来该做什么?
【问题讨论】:
-
在您的自定义单元格类中覆盖tableview单元格的perpareForReuse方法,并在此方法中重置文本字段错误。
-
@vivekDas 感谢您的回复,这是在 objC 中,我尝试覆盖它但没有成功,如果您能告诉我如何在 ObjC 中做这将是很大的帮助。
-
到目前为止您为 perpareForReuse 所尝试的内容显示代码。
标签: ios objective-c uitableview uitextfield