ios - 改变 TableView 或 TableViewCell 的宽度

标签 ios objective-c uitableview interface-builder

大家好 :) 我尝试更改单元格的宽度,因此单元格和表格 View 边框之间有一点空间。我尝试了在stackoverflow上可以找到的所有内容,但没有成功。

我使用界面生成器创建了 tableview,所以首先我尝试将 tableview 大小设置为“自由格式”并将宽度拖到 300.0f,但没有任何结果。比我尝试在我的“viewDidLoad”中以编程方式执行此操作:

self.tableView.frame = CGRectMake(10.0f, self.tableView.frame.origin.y, 300.0f, self.tableView.frame.size.height);

但这里也没有任何 react ......比我尝试直接更改单元格:
- (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];
}

最佳答案

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

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

不确定试试这个希望这对你有帮助

关于ios - 改变 TableView 或 TableViewCell 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21627013/

相关文章:

ios - 在 Swift 中获取当前星期几

ios - NSURLConnection 有问题

objective-c - 释放 NSMutableArray 会导致 EXC_BAD_ACCESS 错误

iphone - 显示搜索时,UITableView 被新的 UITableView 隐藏

iphone - 我的 UIPickerView 具有与键盘相同的行为

ios - iPhone - UILabel 留在 UITableViewCell 之外

ios - 相机应用程序,如屏幕旋转和导航

ios - 标签栏问题: -[UILayoutContainerView hash]: message sent to deallocated instance

java - 如何使用 "US-ASCII"和 HmacSHA256 将 java 代码覆盖到 Objective-C ?

ios - 使用导航显示/消失 UITabbar