gpt4 book ai didi

ios - 表格 View 中重叠的单元格

转载 作者:行者123 更新时间:2023-12-01 16:47:47 25 4
gpt4 key购买 nike

我正在尝试使用单元格和文本字段来实现 UITableView,但单元格不断重叠,文本字段从一个部分跳到另一个部分。

有人可以检查我的代码是否有任何错误吗?

listForItemsinfo =[[NSArray alloc]initWithObjects:@"Name",@"Price", nil];
listForBrandYear =[[NSArray alloc] initWithObjects:@"Year",@"Alfa",@"aston",@"audi",@"bentley",@"BMW",@"Cadillac",@"Chevrolet",@"Ferrari",@"Ford",@"Jaguar",@"Jeep",@"Kia",@"Landrover",@"Mazda",@"Mercedes",@"Mitsubishi",@"Nissan",@"Porshe",@"Subaru", nil];



listForkeywords =[[NSArray alloc] initWithObjects:@"KeyWord1",@"Keyword2", nil];
listForcarType =[[NSArray alloc] initWithObjects:@"Sedan",@"Convertible",@"Coupe",@"Heatchback", nil];
listForItemsCategory =[[NSArray alloc] initWithObjects:@"cat1",@"cat2",@"cat3",@"cat4",@"cat5",@"cat6", nil];

sections = [[NSArray alloc]initWithObjects:@"Item's info",@"Brand, Year",@"Keywords",@"Car Type",@"Category", nil];
listOfsections =[[NSArray alloc]initWithObjects:listForItemsinfo,listForBrandYear,listForkeywords,listForcarType,listForItemsCategory, nil];



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


if (indexPath.section == 0) {

// Add a UITextField
UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 1+ indexPath.row;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];


}else if (indexPath.section == 1 ){
if (indexPath.row==0) {

UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 2+ indexPath.row;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];
}
}else if (indexPath.section == 2 ){
if (indexPath.row==0 | indexPath.row ==1) {

UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 3+ indexPath.row;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
[cell.contentView addSubview:textField];
}else{

}
}
}
//cell.textLabel.text=@"wqewq";
//cell.textLabel.text = [[listOfsections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

[self configureCell:cell atIndexPath:indexPath];



return cell;
}

- (void)configureCell:(UITableViewCell *)theCell atIndexPath:(NSIndexPath *)indexPath {

if (indexPath.section == 0) {

// Get the text field using the tag
UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:1+indexPath.row];
// Position the text field within the cell bounds
CGRect cellBounds = theCell.bounds;
CGFloat textFieldBorder = 10.f;
// Don't align the field exactly in the vertical middle, as the text
// is not actually in the middle of the field.
CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );

textField.frame = aRect;

theCell.textLabel.text = NULL;


// Configure UITextAttributes for each field
if(indexPath.section == 0 & indexPath.row == 0) {
textField.placeholder = @"Name";
textField.returnKeyType = UIReturnKeyNext;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
} else if(indexPath.section == 0 & indexPath.row == 1) {
textField.placeholder = @"Price";
textField.returnKeyType = UIReturnKeyNext;
textField.keyboardType = UIKeyboardTypeNumberPad;
}else if (indexPath.section ==0 & indexPath.row >1){
[textField removeFromSuperview];
}
}else if (indexPath.section == 1) {

// Get the text field using the tag
UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:2+indexPath.row];
// Position the text field within the cell bounds
CGRect cellBounds = theCell.bounds;
CGFloat textFieldBorder = 10.f;
// Don't align the field exactly in the vertical middle, as the text
// is not actually in the middle of the field.
CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );

textField.frame = aRect;

// Configure UITextAttributes for each field
if(indexPath.section == 1 & indexPath.row == 0) {
textField.placeholder = @"Year";
textField.returnKeyType =UIReturnKeyDone;
textField.autocapitalizationType = UIKeyboardTypeNumberPad;
theCell.textLabel.text = NULL;
}else if(indexPath.section == 1 & indexPath.row != 0){
[textField removeFromSuperview];
theCell.textLabel.text = [[listOfsections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

}
}else if (indexPath.section == 2) {

// Get the text field using the tag
UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:3+indexPath.row];
// Position the text field within the cell bounds
CGRect cellBounds = theCell.bounds;
CGFloat textFieldBorder = 10.f;
// Don't align the field exactly in the vertical middle, as the text
// is not actually in the middle of the field.
CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );

textField.frame = aRect;

// Configure UITextAttributes for each field
if(indexPath.section == 2 & indexPath.row == 0) {
textField.placeholder = @"Keyword1";
textField.returnKeyType =UIReturnKeyDone;
textField.autocapitalizationType = UIKeyboardTypeNumberPad;
}else if(indexPath.section == 2 & indexPath.row == 1) {
textField.placeholder = @"Keyword2";
textField.returnKeyType =UIReturnKeyDone;
textField.autocapitalizationType = UIKeyboardTypeNumberPad;
}else{}
}



}

固定的 !如果有人有同样的问题(工作代码)
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Add a UITextField
UITextField *textField = [[UITextField alloc] init];
// Set a unique tag on each text field
textField.tag = 1 + indexPath.row;
textField.delegate=self;
// Add general UITextAttributes if necessary
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
// Position the text field within the cell bounds
CGRect cellBounds = cell.bounds;
CGFloat textFieldBorder = 10.f;
// Don't align the field exactly in the vertical middle, as the text
// is not actually in the middle of the field.
CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f );

textField.frame = aRect;

textField.placeholder = @"Search for an item";
textField.returnKeyType = UIReturnKeyDefault;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

if ((indexPath.section == 0) & (indexPath.row==0)) {

[cell.contentView addSubview:textField];
cell.textLabel.text=NULL;
}else{
[textField removeFromSuperview];
cell.textLabel.text = [[sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}

最佳答案

该表不会使单元格重叠,但单元格会被重用。您的问题是您在初始化单元时唯一配置单元。事实上,无论是创建新单元还是从 -dequeueReusableCellWithIdentifier: 获取旧单元,都需要进行该配置。 .

也许您认为表格 View 会保存您提供的每个单元格?这不是它的工作原理......可重复使用的单元格意味着一个表格只需要一次可以显示的单元格。当您 ScrollView 时,从顶部或底部滚动的单元格将作为新内容的新单元格回收。但是,您没有设置新内容,而是显示旧内容。

if 的第一行之后添加右括号,以便始终执行其余代码:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

if (indexPath.section == 0) {

关于ios - 表格 View 中重叠的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18557740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com