Ios 更改TableView或TableViewCell的宽度

Ios 更改TableView或TableViewCell的宽度,ios,objective-c,uitableview,interface-builder,Ios,Objective C,Uitableview,Interface Builder,大家好:)我尝试更改单元格的宽度,以便在单元格和tableview边框之间留出一点空间。我在stackoverflow上尝试了我能找到的一切,但没有成功 我用interface builder创建了tableview,因此首先我尝试将tableview大小设置为“freeform”,并将宽度拖动到300.0f,但没有结果。然后我尝试在我的“viewDidLoad”中以编程方式使用: 但这里也没有发生任何事情。。。。然后我尝试直接用以下方法更改单元格: - (UITableViewCell *)t

大家好:)我尝试更改单元格的宽度,以便在单元格和tableview边框之间留出一点空间。我在stackoverflow上尝试了我能找到的一切,但没有成功

我用interface builder创建了tableview,因此首先我尝试将tableview大小设置为“freeform”,并将宽度拖动到300.0f,但没有结果。然后我尝试在我的“viewDidLoad”中以编程方式使用:

但这里也没有发生任何事情。。。。然后我尝试直接用以下方法更改单元格:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

newsCell.contentView.frame = CGRectMake(10.0f, 0, 300, newsCell.frame.size.height);
}

但这里也有同样的问题……有什么想法我遗漏了什么吗

编辑:此问题的另一个解决方案是使用以下内容更改自定义单元格的框架:

- (void)setFrame:(CGRect)frame {
    frame.origin.x += inset;
    frame.size.width -= 2 * inset;
    [super setFrame:frame];
}
使用以下命令:

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 

委派
UITableView
的方法,并返回浮点值(CellHight+空格bw cells)。

为此,首先可以使用UIImageView覆盖整个视图,并将其图像设置为带边框的图像。现在在此imageview上添加一个表视图,使其具有宽度,以便此图像的边框可见。

我认为您希望Tableviewcell具有动态高度,而不是宽度

UITableView的委托方法将有助于:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
它将返回每个单元格的高度。您可以按照以下示例代码实现它。这是在动态文本内容的基础上显示动态高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  //set width depending on device orientation
  self.cellPrototype.frame = CGRectMake(self.cellPrototype.frame.origin.x,     self.cellPrototype.frame.origin.y, tableView.frame.size.width, self.cellPrototype.frame.size.height);

 CGFloat quotationLabelHeight = [self sizeOfLabel:self.cellPrototype.quotationLabel withText:[self quotationTextForRow:indexPath.row]].height;
CGFloat attributionLabelHeight = [self sizeOfLabel:self.cellPrototype.attributionLabel withText:[self attributionTextForRow:indexPath.row]].height;
CGFloat padding = self.cellPrototype.quotationLabel.frame.origin.y;

CGFloat combinedHeight = padding + quotationLabelHeight + padding/2 + attributionLabelHeight + padding;
CGFloat minHeight = padding + self.cellPrototype.avatarButton.frame.size.height + padding;

return MAX(combinedHeight, minHeight);
}
你可以试试。

试试这个 在自定义单元格中放置一个属性,如

   in .h file
   @interface GTNewsCustomCell : UITableViewCell
   @property (nonatomic, assign)CGRect cellFrame;

   in .m file
   - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
   { 
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) {
       // Initialization code
       self.backgroundColor = [UIColor greenColor];//for testing purpose only
   } 
   return self;
 }

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

       // Configure the view for the selected state
   }

    //automatically called 
   - (void)layoutSubviews
  {
      [super layoutSubviews];
       CGRect cellRect = self.bounds;
       cellRect.size.width = self.cellFrame.size.width;

       self.bounds = cellRect;

  }

   .in .m of viewController

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        GTNewsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if(cell == nil)
        {
            cell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
       cell.cellFrame = CGRectMake(10, 0, tableRect.size.width,40);//hear tableRect is the frame of your tableview
       return cell;
   }

不确定是否尝试此操作希望这有助于u

默认情况下,表格边缘和单元格之间没有空格。您是否对边框未到达边缘感到困惑?如果要将标签和图像添加到此单元格,我建议使用一个UIVIEW,然后将标签和图像添加到其中,并将子视图添加到cell.contentview。根据需要更改UIVIEW的框架。希望能有帮助!!使用自动布局。忘了框架吧。我想他问的是宽度而不是高度。
   in .h file
   @interface GTNewsCustomCell : UITableViewCell
   @property (nonatomic, assign)CGRect cellFrame;

   in .m file
   - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
   { 
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) {
       // Initialization code
       self.backgroundColor = [UIColor greenColor];//for testing purpose only
   } 
   return self;
 }

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

       // Configure the view for the selected state
   }

    //automatically called 
   - (void)layoutSubviews
  {
      [super layoutSubviews];
       CGRect cellRect = self.bounds;
       cellRect.size.width = self.cellFrame.size.width;

       self.bounds = cellRect;

  }

   .in .m of viewController

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        GTNewsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if(cell == nil)
        {
            cell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        }
       cell.cellFrame = CGRectMake(10, 0, tableRect.size.width,40);//hear tableRect is the frame of your tableview
       return cell;
   }