【发布时间】:2017-05-20 09:23:38
【问题描述】:
我有一个表格视图来复制表单页面。它有两个部分,第 0 部分有 8 个单元格,其他有 4 个单元格
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [tableViewSectionData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section)
{
case 0:
return 8;
break;
case 1:
return 4;
break;
default:
return 0;
break;
}
}
一些单元格用于显示选择器,我通过在单元格内的文本字段上添加一个按钮并在必要时启用然后启用。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
TextFieldCell *cellInSection0 = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCellInSection0" forIndexPath:indexPath];
if (!cellInSection0)
{
cellInSection0 = [[TextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TextFieldCellInSection0"];
}
cellInSection0.bottomButton.hidden = YES;
cellInSection0.textFieldBottomLabel.alpha = 0.5;
cellInSection0.textFieldImageView.alpha = 0.5;
cellInSection0.textfield.delegate = self;
cellInSection0.textfield.tag = indexPath.row;
cellInSection0.pickerViewButton.hidden = YES;
NSString *placeholderString;
NSString *imageName;
switch (indexPath.row)
{
case 0:
{
if (fname)
{
cellInSection0.textfield.text = fname;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
}
else
{
placeholderString = @"FIRST NAME";
}
imageName = @"user";
}
break;
case 1:
{
if (lname)
{
cellInSection0.textfield.text = lname;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
}
else
{
placeholderString = @"LAST NAME";
}
imageName = @"user";
}
break;
case 2:
{
if (address)
{
cellInSection0.textfield.text = address;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
}
else
{
placeholderString = @"ADDRESS";
}
imageName = @"address";
}
break;
case 3:
{
cellInSection0.textfield.returnKeyType = UIReturnKeyDone;
if (floor_unit)
{
cellInSection0.textfield.text = floor_unit;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
}
else
{
placeholderString = @"FLOOR/UNIT";
}
imageName = @"address";
}
break;
case 4:
{
cellInSection0.pickerViewButton.hidden = NO;
cellInSection0.textfield.enabled = NO;
cellInSection0.pickerViewButton.tag = indexPath.row;
[cellInSection0.pickerViewButton addTarget:self action:@selector(showCountryPicker:) forControlEvents:UIControlEventTouchUpInside];
if (countryName)
{
cellInSection0.textfield.text = countryName;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
cellInSection0.textfield.alpha = 1.0;
}
else
{
cellInSection0.textfield.text = @"COUNTRY";
cellInSection0.textfield.alpha = 0.5;
}
imageName = @"mapicon";
}
break;
case 5:
{
cellInSection0.pickerViewButton.hidden = NO;
cellInSection0.textfield.enabled = NO;
cellInSection0.pickerViewButton.tag = indexPath.row;
[cellInSection0.pickerViewButton addTarget:self action:@selector(showStatePicker:) forControlEvents:UIControlEventTouchUpInside];
if (stateName)
{
cellInSection0.textfield.text = stateName;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
cellInSection0.textfield.alpha = 1.0;
}
else
{
cellInSection0.textfield.text = @"STATE";
cellInSection0.textfield.alpha = 0.5;
}
imageName = @"mapicon";
}
break;
case 6:
{
cellInSection0.pickerViewButton.hidden = NO;
cellInSection0.textfield.enabled = NO;
cellInSection0.pickerViewButton.tag = indexPath.row;
[cellInSection0.pickerViewButton addTarget:self action:@selector(showCityPicker:) forControlEvents:UIControlEventTouchUpInside];
if (cityName)
{
cellInSection0.textfield.text = cityName;
cellInSection0.textFieldBottomLabel.alpha = 1.0;
cellInSection0.textFieldImageView.alpha = 1.0;
cellInSection0.textfield.alpha = 1.0;
}
else
{
cellInSection0.textfield.text = @"CITY";
cellInSection0.textfield.alpha = 0.5;
}
imageName = @"mapicon";
}
break;
case 7:
{
placeholderString = @"ZIP";
imageName = @"mapicon";
}
break;
default:
break;
}
if (placeholderString)
{
NSAttributedString *placeHolder = [[NSAttributedString alloc] initWithString:placeholderString attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
cellInSection0.textfield.attributedPlaceholder = placeHolder;
}
cellInSection0.textFieldImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", imageName]];
return cellInSection0;
}
else
{
TextFieldCell *cellInSection1 = [tableView dequeueReusableCellWithIdentifier:@"TextFieldCellInSection1" forIndexPath:indexPath];
if (!cellInSection1)
{
cellInSection1 = [[TextFieldCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TextFieldCellInSection1"];
}
cellInSection1.bottomButton.hidden = YES;
cellInSection1.textFieldBottomLabel.alpha = 0.5;
cellInSection1.textFieldImageView.alpha = 0.5;
cellInSection1.textfield.delegate = self;
cellInSection1.textfield.tag = indexPath.row;
NSString *placeholderString;
NSString *imageName;
switch (indexPath.row)
{
case 0:
{
if (cardNumber)
{
cellInSection1.textfield.text = cardNumber;
}
else
{
placeholderString = @"CARD NO.";
}
imageName = @"yearofincorporationicon";
}
break;
case 1:
{
if (month)
{
cellInSection1.textfield.text = month;
}
else
{
placeholderString = @"MONTH";
}
imageName = @"calender";
}
break;
case 2:
{
if (year)
{
cellInSection1.textfield.text = year;
}
else
{
placeholderString = @"YEAR";
}
imageName = @"calender";
}
break;
case 3:
{
if (cvv)
{
cellInSection1.textfield.text = cvv;
}
else
{
placeholderString = @"CVV";
}
imageName = @"yearofincorporationicon";
}
break;
default:
break;
}
if (placeholderString)
{
NSAttributedString *placeHolder = [[NSAttributedString alloc] initWithString:placeholderString attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
cellInSection1.textfield.attributedPlaceholder = placeHolder;
}
cellInSection1.textFieldImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", imageName]];
return cellInSection1;
}
}
乍一看,这很有效。但是当我在文本字段中输入一些文本(例如第 0 节中的文本字段第一个)时,第 1 节的第一个文本字段也显示相同的文本。这发生在每个文本字段中。有时占位符字符串也不匹配。它发生得如此随机,我无法找到问题所在。
任何帮助都很好。 谢谢
【问题讨论】:
标签: ios objective-c uitableview uitextfield tableviewcell