我有一個異構tableView與整個部分組成的UITableViewCells與UITextField控件在每個人的contetView。問題在於,當我滾動時,我看到文本字段中的文本被混合在一起。UITableView與自定義UITableViewCells混合滾動內容
在這個問題出現之前,我已經見過很多次了,雖然已經廢棄了,但我通過爲每種單元格分配了不同的cellIdentifier來解決這個問題。
現在有什麼不同呢是我動態地創建單元格,並且根據服務器的答案,表格部分可能有零個或多個自定義單元格(具有textField的單元格)。現在
,這很有趣地看到,textfiled的屬性,如佔位符是整個卷軸執着,但我寫的文字不具有持續性可言,文字改變它的位置...
我有一個視頻here所以你可以看到我的意思。
我使用的代碼是這樣的:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
NSString *varCellID = [NSString stringWithFormat:@"%i_VarCellID", (indexPath.row + 7891)];
if (indexPath.section == 0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self configureCellViews:cell indexPath:indexPath];
}
[self configureCellContent:cell indexPath:indexPath];
return cell;
}
else if (indexPath.section == 1) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[self configureCellViews:cell indexPath:indexPath];
}
[self configureCellContent:cell indexPath:indexPath];
return cell;
}
else {
UITableViewCell *varCell = [tableView dequeueReusableCellWithIdentifier:varCellID];
if (varCell == nil) {
varCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:varCellID] autorelease];
varCell.selectionStyle = UITableViewCellSelectionStyleNone;
[self configureCellViews:varCell indexPath:indexPath];
}
[self configureCellContent:varCell indexPath:indexPath];
return varCell;
}
}
我知道這不是優雅,但woked之前。除了dinamically創建的單元格標識符是我(絕望)嘗試在每個textField上保留單獨內容之一。
下面是如何創建單元格的觀點和內容:
#pragma mark -
#pragma mark ConfigureCellcontent
- (void)configureCellViews:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
NSArray *aSection = [sections objectAtIndex:indexPath.section];
NSDictionary *aField;
int varformatid;
if (indexPath.section == 0) {
cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
}
else if (indexPath.section == 1) {
cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
}
else {
aField = [aSection objectAtIndex:indexPath.row];
varformatid = [[aField valueForKey:@"varformatid"] intValue];
NSArray *aSection = [sections objectAtIndex:indexPath.section];
NSLog(@"format id %@", [[aSection objectAtIndex:0] valueForKey:@"varname"]);
fieldLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 7, 100, 30)];
fieldLabel.tag = 1234;
fieldLabel.textAlignment = UITextAlignmentLeft;
fieldLabel.font = [UIFont boldSystemFontOfSize:14];
fieldLabel.adjustsFontSizeToFitWidth = YES;
[cell.contentView addSubview:fieldLabel];
theTextField = [[UITextField alloc] initWithFrame:CGRectMake(130, 12, 170, 30)];
theTextField.textColor = kAnswersTextColor;
theTextField.returnKeyType = UIReturnKeyDone;
theTextField.tag = indexPath.row + 101;
theTextField.adjustsFontSizeToFitWidth = YES;
theTextField.delegate = self;
if(varformatid == 6 || varformatid == 7 || varformatid == 8) {
// use number pad input
theTextField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview: theTextField];
[theTextField release];
}
else if(varformatid == 4) {
// use date
}
else if(varformatid == 17 || varformatid == 18) {
// use pull down, elements of pull down are in XML
}
else if(varformatid == 20) {
theSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(120, 8, 100, 30)];
theSwitch.selected = YES;
[cell.contentView addSubview:theSwitch];
}
else {
// use input string
[cell addSubview: theTextField];
[theTextField release];
}
}
}
- (void)configureCellContent:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
NSArray *aSection = [sections objectAtIndex:indexPath.section];
NSString *aField;
if (indexPath.section == 0) {
aField = [[aSection objectAtIndex:0] valueForKey:@"SetDate"];
cell.textLabel.text = @"Set Date";
cell.detailTextLabel.text = aField;
}
else if (indexPath.section == 1) {
aField = [[aSection objectAtIndex:0] valueForKey:@"SetType"];
cell.textLabel.text = @"Set Type";
cell.detailTextLabel.text = aField;
}
else {
aField = [[aSection objectAtIndex:indexPath.row] valueForKey:@"varname"];
if ([[[aSection objectAtIndex:indexPath.row] valueForKey:@"varrequired"] isEqualToString:@"yes"]) {
[(UITextField *)[cell viewWithTag:indexPath.row + 101] setPlaceholder: @"Required"];
}
else [(UITextField *)[cell viewWithTag:indexPath.row + 101] setPlaceholder: @"Optional"];
[(UILabel *)[cell.contentView viewWithTag:1234] setText: aField];
}
}
最後,我請求你的幫助,以找到至少一個更好的辦法。提前致謝。
謝謝,這幫了我,現在我對輸入的文本有了更多的控制。每次爲每個textField調用textFieldDidEndEditing:方法時,現在我都有相同數量的節和行的數組來存儲文本。儘管如此,我仍然可以看到內容混雜,只有在第一行和最後一行之間纔會發生。 – chuckSaldana 2010-08-19 19:49:02