简体   繁体   中英

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

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 . So now in tableview cell you know the row, based on row number make the changes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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