简体   繁体   English

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

[英]Change UITextfield width on custom UITableviewCell in iOS7

I am using grouped UITableview with custom cell.My tableview consists of two sections.I need to change textfield frame only for two rows in section zero.How to possible??Please help me.Go through my code 我正在使用带有自定义单元格的分组UITableview。我的tableview由两部分组成。我只需要在零部分的两行中更改文本字段框架。如何可能?请帮助我。遍历我的代码

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

}

Insted of this make a property so that u can set the specified frame u want hear the code, in controller u need to set frame for each cell see the code below 为此插入一个属性,以便您可以设置指定的帧您想听代码,在控制器中您需要为每个单元格设置帧,请参见下面的代码


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


Create a custom table view cell initializer and pass indexpath . 创建一个自定义表格视图单元格初始化程序,并传递indexpath。 So now in tableview cell you know the row, based on row number make the changes 现在,在tableview单元格中,您知道行,基于行号进行更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM