2015-11-02 60 views
-1

我已經創建了UITableView以編程方式,最重要的是我有UITableVIewCellUITextFieldUILabel(在附圖中標記爲綠色圓圈)。當我滾動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; 
} 

截圖

TableView screenshot

回答

1

如果從dequeueReusableCellWithIdentifier時返回小區不是零,這意味着它是被重複使用現有的細胞。你的代碼添加子視圖和任何其他初始化,你只想對新創建的單元格執行一次操作,它應該位於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; 
} 
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; 
+0

您必須將整個控制移動到細胞==零如果條件part.then只有你的代碼工作 – Bangalore

+1

@Bangalore遺憾,錯過了牙套! – Rumin

相關問題