2014-02-05 51 views
0

我正在使用自定義單元格對分組的UITableview。我的tableview由兩部分組成。我需要更改第0部分中的兩行的textfield框架。如何可能?請幫助我。通過我的這個代碼在iOS7中更改自定義UITableviewCell上的UITextfield寬度

customcell.m

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 

     self.textfiled1 = [[UITextField alloc]init]; 
     self.textfiled1.returnKeyType = UIReturnKeyNext; 
     textfiled1.clearButtonMode = UITextFieldViewModeWhileEditing; 

     [self.contentView addSubview:self.textfiled1]; 

    } 
    return self; 
} 
- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 
} 
-(void)layoutSubviews{ 

    [super layoutSubviews]; 
    self.textfiled1.frame = CGRectMake(50, 3, 250,40); 
} 

#pragma Tableview Delegate 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath 
{ 
    if(indexPath.section == 0) 
    { 
     static NSString *CellIdentifier = @"Cell"; 
     customcell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
      cell = [[customcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
      cell.selectionStyle = UITableViewCellSeparatorStyleNone; 
     } 
     cell.textfiled1.delegate = self; 
     if (indexPath.row==0) { 

      cell.textfiled1.frame = CGRectMake(50,3,180,40);//Change textfield frame 

      cell.separatorInset = UIEdgeInsetsMake(0, 50, 0, 100); 

     } 
     else if(indexPath.row==1) 
     { 
      cell.textfiled1.frame = CGRectMake(50,3,180,40);//Change textfield frame 

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

      cell.textfiled1.keyboardType = UIKeyboardTypePhonePad; 

     } 
     else if(indexPath.row==3) 
     { 

     } 
     return cell; 
    } 
    else if(indexPath.section == 1) 
    { 
     static NSString *CellIdentifier1 = @"Cell1"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 

     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]; 
     } 
     cell.textLabel.text = @「Section1"; 
     return cell; 
    } 

} 
+0

出現了什麼問題?你有什麼錯誤嗎?或txtField幀沒有得到改變? – Nayan

+0

@Nayan,第0和第1行的textfield frame沒有改變。爲什麼? – IKKA

+0

因爲你在' - (void)layoutSubviews'方法中設置了恆定幀,這就是wy,看到我的答案希望它有幫助 –

回答

0

insted的進入使屬性好讓你可以設置指定的幀u想聽到的代碼,在控制器u需要設置幀爲每個單元格中看到的代碼如下


//CustomCell.h file 

@interface CustomCell : UITableViewCell 
@property(nonatomic,assign) CGRect TextFieldFrame;//put this to change the frame 
@property (nonatomic,retain)UITextField *textfiled1; 
@end 

//in CustomCell.m file 
#import "CustomCell.h" 

@implementation CustomCell 
@synthesize TextFieldFrame;//sysnthesise 
@synthesize textfiled1; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
      self.textfiled1 = [[UITextField alloc]init]; 
      self.textfiled1.backgroundColor = [UIColor greenColor]; 
      self.textfiled1.returnKeyType = UIReturnKeyNext; 
      textfiled1.clearButtonMode = UITextFieldViewModeWhileEditing; 
      [self.contentView addSubview:self.textfiled1]; 
     } 
    return self; 
} 


-(void)layoutSubviews{ 

    [super layoutSubviews]; 
    self.textfiled1.frame = CGRectMake(self.bounds.origin.x + self.TextFieldFrame.origin.x, self.bounds.origin.y + self.TextFieldFrame.origin.y, self.TextFieldFrame.size.width, self.TextFieldFrame.size.height); 
    // self.textfiled1.frame = CGRectMake(50, 3, 250,40);//hear u are setting the contant frame thats wy frames are not changed 
    } 

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated 
    { 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
    } 


    //in ViewController.m file 
#import "ViewController.h" 
#import "CustomCell.h" 

@interface ViewController()<UITableViewDataSource,UITableViewDelegate> 

@end 

@implementation ViewController 


    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 2; 
} 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
     return 2; 
} 


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(indexPath.section == 0) 
    { 
     static NSString *CellIdentifier = @"Cell"; 
     CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) { 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSeparatorStyleNone; 
     } 
     cell.textfiled1.delegate = self; 
     if (indexPath.row==0) { 

     cell.TextFieldFrame = CGRectMake(50,3,180,40);//Change textfield frame, u can set the frame for each cell hear 

     cell.separatorInset = UIEdgeInsetsMake(0, 50, 0, 100); 

     } 
     else if(indexPath.row==1) 
     { 
      cell.TextFieldFrame = CGRectMake(50,3,180,40);//Change textfield frame, heare also 

     } 
     return cell; 
    } 
    else if (indexPath.section == 1) 
    { 
    static NSString *CellIdentifier = @"Cell_2"; 
    CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSeparatorStyleNone; 
     } 
     cell.textfiled1.delegate = self; 
     if(indexPath.row==0) 
    { 

     cell.textfiled1.keyboardType = UIKeyboardTypePhonePad; 
     cell.TextFieldFrame = CGRectMake(80, 3, 180, 40); //for other cells default frame u need to set it heare 

    } 
     else if(indexPath.row==1) 
    { 
     cell.textfiled1.keyboardType = UIKeyboardTypePhonePad; 
     cell.TextFieldFrame = CGRectMake(80, 3, 180, 40); 

    } 
    return cell; 

} 
    else 
     return nil; 

} 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 50; 
} 


0

創建自定義表視圖細胞初始化和indexpath通過。所以現在在tableview單元格中你知道該行,根據行號進行更改

相關問題