在ios7中的自定义UITATIONVIEWCELL上更改UITEXTFIELD宽度



我正在使用自定义单元格进行分组的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;
    }
}

所设立的属性,以便您可以设置指定的帧,您想要听到代码,在控制器中,您需要为每个单元格设置帧,请参见下面的代码


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


创建自定义表视图单元初始化器并通过Indexpath。因此,现在在tableview单元格中,您知道该行,基于行号进行更改

最新更新