我已经以编程方式创建了UITableView
,最重要的是UITableVIewCell
UITextField
和UILabel
(在附加图片中标记为绿色圆圈)。当我滚动UITableView
时,我可以看到隐藏的UITableViewCell
(底部单元格)部分和UITextField
位置已更改(在附加图像中标记为红色圆圈。
请告诉我如何确保UITableViewCell
中的所有控件都能正确对齐?
绿色圆圈正确,红色圆圈错误。
我的代码
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [selectedTabFields count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"cell";
NSLog(@"%@",[selectedTabFields objectAtIndex:indexPath.row]);
cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[homeCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
//cell.textLabel.text = @"My Text";
cell.selectionStyle=UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:[self getLabel:indexPath.row]];
if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
[cell.contentView addSubview:[self getVarcharTextfield:indexPath.row]];
}else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){
[cell.contentView addSubview:[self getIntegerTextfield:indexPath.row]];
}
return cell;
}
-(UILabel *)getLabel:(NSUInteger)index{
UILabel *slogan= [[UILabel alloc] initWithFrame:CGRectMake(15,0,cell.frame.size.width,cell.frame.size.height)];
//slogan.text=[[selectedTabFields valueForKey:@"name"] objectAtIndex:index];
slogan.text=@"text Label";
slogan.textAlignment=UITextAlignmentLeft;
slogan.font= [UIFont fontWithName:@"Helvetica-Neue Light" size:12];
slogan.font=[slogan.font fontWithSize:12];
slogan.textColor=[UIColor lightGrayColor];
slogan.backgroundColor=[UIColor clearColor];
return slogan;
}
//Varchar Textfield
-(UITextField *)getVarcharTextfield:(NSUInteger)index{
UITextField *textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
textField.placeholder=@"Text here";
textField.tag=index;
textField.textAlignment=UITextAlignmentLeft;
textField.font= [UIFont fontWithName:@"Helvetica-Neue Light" size:14];
textField.textColor=[UIColor darkGrayColor];
textField.backgroundColor=[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
return textField;
}
//Integer Textfield
-(UITextField *)getIntegerTextfield:(NSUInteger)index{
UITextField *textField= [[UITextField alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
textField.placeholder=@"Text here";
textField.tag=index;
textField.keyboardType=UIKeyboardTypeNumberPad;
textField.textAlignment=UITextAlignmentLeft;
textField.font= [UIFont fontWithName:@"Helvetica-Neue Light" size:14];
textField.textColor=[UIColor darkGrayColor];
textField.backgroundColor=[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1];
return textField;
}
截图
答案 0 :(得分:1)
您需要在滚动时移除子视图,因为每次调用cellForRowAtIndexPath:
时它都会继续添加。
if (cell == nil)
{
cell = [[homeCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
//cell.textLabel.text = @"My Text";
cell.selectionStyle=UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:[self getLabel:indexPath.row]];
if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
[cell.contentView addSubview:[self getVarcharTextfield:indexPath.row]];
}else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){
[cell.contentView addSubview:[self getIntegerTextfield:indexPath.row]];
}
}
else
{
[[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
return cell;
答案 1 :(得分:1)
如果从dequeueReusableCellWithIdentifier
返回时单元格不是nil,则表示它是重用的现有单元格。将您只想对新创建的单元格执行一次子视图和任何其他初始化的代码应该放在if (cell == nil) {
部分中。
此外,您应考虑为两种不同的细胞类型使用不同的细胞重用类型,并将这些子视图仅添加到新创建的细胞中。所以请@"IntCell"
和@"CharCell"
根据您的上述代码:
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier;
if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
MyIdentifier = @"CharCell";
}
}else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){
MyIdentifier = @"IntCell";
}
NSLog(@"%@",[selectedTabFields objectAtIndex:indexPath.row]);
cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[homeCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
//cell.textLabel.text = @"My Text";
cell.selectionStyle=UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:[self getLabel:indexPath.row]];
if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
[cell.contentView addSubview:[self getVarcharTextfield:indexPath.row]];
}else if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"INTEGER"]){
[cell.contentView addSubview:[self getIntegerTextfield:indexPath.row]];
}
}
return cell;
}