我已經創建了UITableView
以編程方式,最重要的是我有UITableVIewCell
與UITextField
和UILabel
(在附圖中標記爲綠色圓圈)。當我滾動UITableView
我可以看到我的隱藏UITableViewCell
(底部細胞)和UITextField
位置的部分改變(標記爲附加的圖像紅色的圓圈。以編程方式創建的UITablevIew在滾動時更改其UITextField位置 - iOS?
請告訴我如何才能確保在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];
[email protected]"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)];
[email protected]"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)];
[email protected]"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;
}
截圖
您必須將整個控制移動到細胞==零如果條件part.then只有你的代碼工作 – Bangalore
@Bangalore遺憾,錯過了牙套! – Rumin