我在动态创建的自定义 UITableViewCells 上有 UITextFields。我正在使用它们来输入数据(或者如果它是一个编辑,则从核心数据中提取它)并将数据写回到editingDidEnd。
我的问题是我可以选择 UITextFields 到我的心脏内容,但如果他们滚动屏幕并再次打开,我无法触摸 UITextField。
如果没有数据并且 textField 中有数据,则会发生这种情况。当单元格重新滚动时,数据仍然存在。
有趣的是,如果我触摸 textField,调出键盘,将单元格滚动到屏幕外,然后再滚动,然后按键盘上的返回键,我可以再次选择单元格。因此,当 UITextfield 是 theFirstResponder 时滚动单元格似乎可以保护它免受我在这里做错的任何事情的影响。
投入几行代码,我确定我的触摸在没有触摸单元格上的 UITextField 时触发了 didSelectRowATIndexPath。
我正在使用情节提要,我似乎正确地使用了 cellIdentifiers
有没有人经历过这种行为并对我应该去哪里提出建议?
任何建议都将不胜感激,因为在这一点上,我已经花了将近 3 天的时间将头撞在墙上,试图弄清楚发生了什么
谢谢你。
danypata 继承了 cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ NSString *CellIdentifier;
if (indexPath.section == dLocation){
CellIdentifier=@"FacilityAddressCell";
}else {
CellIdentifier=@"FacilityGPSCell";
}
DLog(@"****CellIdentifier = %@",CellIdentifier);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
switch (indexPath.section) {
case dLocation:
{
FacilityAddressCell *theFacilityAddressCell = (FacilityAddressCell*) cell;
theFacilityAddressCell.facilityAddressField.placeholder = self.locationFieldNames[indexPath.row];
theFacilityAddressCell.accessoryType =UITableViewCellAccessoryNone;
theFacilityAddressCell.selectionStyle = UITableViewCellSelectionStyleNone;
switch (indexPath.row) {
case facilityName:
theFacilityAddressCell.facilityAddressField.text = self.currentOperation.name;
break;
case webSiteUrl:
DLog(@"self.cur.website = '%@'",self.currentOperation.website);
theFacilityAddressCell.facilityAddressField.text = self.currentOperation.website;
break;
case emailAddress:
DLog(@"self.cur.email = '%@'",self.currentOperation.email);
theFacilityAddressCell.facilityAddressField.text = self.currentOperation.email;
break;
case country:
DLog(@"self.cur.country = '%@'",self.currentOperation.country);
theFacilityAddressCell.facilityAddressField.enabled = NO;
theFacilityAddressCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
theFacilityAddressCell.facilityAddressField.text = self.currentOperation.country;
break;
case streetName:
DLog(@"self.cur.address = '%@'",self.currentOperation.address);
DLog(@"streetnames initial value is '%@'",self.currentOperation.address);
theFacilityAddressCell.facilityAddressField.text = self.currentOperation.address;
break;
case areaName:
DLog(@"self.cur.address2 = '%@'",self.currentOperation.address2);
theFacilityAddressCell.facilityAddressField.text = self.currentOperation.address2;
break;
case island:
DLog(@"self.cur.address3 = '%@'",self.currentOperation.address3);
theFacilityAddressCell.facilityAddressField.text = self.currentOperation.address3;
break;
case postCode:
DLog(@"self.cur.postcode ='%@'",self.currentOperation.postcode);
theFacilityAddressCell.facilityAddressField.text = self.currentOperation.postcode;
break;
case phoneNumber:
DLog(@"self.cur.phone ='%@'",self.currentOperation.phoneNumber);
theFacilityAddressCell.facilityAddressField.text = self.currentOperation.phoneNumber;
break;
case faxNumber:
DLog(@"self.cur.fax ='%@'",self.currentOperation.faxNumber);
theFacilityAddressCell.facilityAddressField.text = self.currentOperation.faxNumber;
break;
case tapToBringForward:
theFacilityAddressCell.facilityAddressField.enabled=NO;
theFacilityAddressCell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
theFacilityAddressCell.facilityAddressField.text =@"Use last location details";
break;
default:
break;
}
cell= theFacilityAddressCell;
}
break;
case dGPSLoc:
{
DLog( @"loading the [cell data");
GPSLocCell *theGPSLocCell = (GPSLocCell *)cell;
theGPSLocCell.latLabel.text= [self.currentOperation.latitude stringValue];
theGPSLocCell.longLabel.text=[self.currentOperation.longitude stringValue];
theGPSLocCell.lockedOrNotLabel.text = @"Locked";
cell= theGPSLocCell;
}
break;
}
return cell;
}
解决方案 - 问题是我正在重用单元格,其中一些将 textfield.enabled 设置为 NO,并且它们以 NO 作为起始值被重用。多哈。希望我的愚蠢可以帮助其他人。
经验教训 - 当您重用 UITableViewCell 或其子类时,它们会保留以前使用的设置,如果您有相同重用类型的某些单元格的格式不同,您最好确保将它们全部设置在 cellForIndexPath 中 - 不仅仅是例外:)