在iOS7中更改自定义UITableviewCell上的UITextfield宽度

在iOS7中更改自定义UITableviewCell上的UITextfield宽度,ios,iphone,objective-c,uitableview,ios7,Ios,Iphone,Objective C,Uitableview,Ios7,我正在将分组UITableview与自定义单元格一起使用。我的tableview由两个部分组成。我只需要更改第0部分中两行的textfield框架。如何可能?请帮助我。检查我的代码 customcell.m -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier

我正在将分组UITableview与自定义单元格一起使用。我的tableview由两个部分组成。我只需要更改第0部分中两行的textfield框架。如何可能?请帮助我。检查我的代码

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;
    }

}

除此之外,创建一个属性,以便您可以设置指定的帧,以便听到代码。在控制器中,您需要为每个单元格设置帧,请参见下面的代码


 //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;
}
//CustomCell.h文件
@接口CustomCell:UITableViewCell
@属性(非原子,赋值)CGRect TextFieldFrame//把这个换一下镜框
@属性(非原子,保留)UITextField*textfiled1;
@结束
//在CustomCell.m文件中
#导入“CustomCell.h”
@实现自定义单元
@综合文本框//辛瑟斯
@合成文本文件1;
-(id)initWithStyle:(UITableViewCellStyle)样式重用标识符:(NSString*)重用标识符
{
self=[super-initWithStyle:style-reuseIdentifier:reuseIdentifier];
如果(自我){
self.textfiled1=[[UITextField alloc]init];
self.textfiled1.backgroundColor=[UIColor-greenColor];
self.textfiled1.returnKeyType=UIReturnKeyNext;
textfiled1.clearButtonMode=UITextFieldViewModewhileEdit;
[self.contentView addSubview:self.textfiled1];
}
回归自我;
}
-(无效)布局子视图{
[超级布局子视图];
self.textfiled1.frame=CGRectMake(self.bounds.origin.x+self.TextFieldFrame.x,self.bounds.origin.y+self.TextFieldFrame.origin.y,self.TextFieldFrame.size.width,self.TextFieldFrame.size.height);
//self.textfiled1.frame=CGRectMake(50,3250,40);//听说您正在设置连续帧,但wy帧没有更改
}
-(无效)设置选定:(BOOL)选定动画:(BOOL)动画
{
[超级设置选定:选定动画:动画];
//为所选状态配置视图
}
//在ViewController.m文件中
#导入“ViewController.h”
#导入“CustomCell.h”
@界面视图控制器()
@结束
@实现视图控制器
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回2;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回2;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.section==0)
{
静态NSString*CellIdentifier=@“Cell”;
CustomCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle=UITableViewCellSeparatorStyleNone;
}
cell.textfiled1.delegate=self;
if(indexPath.row==0){
cell.TextFieldFrame=CGRectMake(50,3180,40);//更改textfield frame,您可以为每个单元格设置帧
cell.separatorInset=UIEdgeInsetsMake(0,50,0,100);
}
else if(indexPath.row==1)
{
cell.TextFieldFrame=CGRectMake(50,3180,40);//更改textfield帧,也可以
}
返回单元;
}
else if(indexPath.section==1)
{
静态NSString*CellIdentifier=@“Cell_2”;
CustomCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==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);//对于其他单元格,需要设置默认帧
}
else if(indexPath.row==1)
{
cell.textfiled1.keyboardType=UIKeyboardTypePhonePad;
cell.TextFieldFrame=CGRectMake(80,3,180,40);
}
返回单元;
}
其他的
返回零;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径
{
返回50;
}


创建自定义表视图单元格初始值设定项并传递indexath。因此,现在在tableview单元格中,您知道了行,根据行号进行更改

发生了什么问题??你有什么错误吗?或者txtField框架没有改变???@Nayan,第0节第1行和第2行的textfield框架没有改变。为什么?因为你在
-(void)layoutSubviews
方法中设置了常量框架,这是wy,请参见我的答案helps@Shan,非常感谢。。
 //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;
}