改变TableView或TableViewCell的宽度

嘿大家:)我尝试改变我的单元格的宽度,所以单元格和tableview边框之间有一个小空间。 我尝试了一切,我可以在这里findstackoverflow,但没有成功。

我使用界面生成器创build了tableview,所以首先我简单地尝试将tableview大小设置为“自由forms”,并将宽度拖到300.0f,但没有任何反应。 比我试图以编程方式在我的“viewDidLoad”与:

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

但在这里也没有任何反应….比我试图直接更改单元格:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; newsCell.contentView.frame = CGRectMake(10.0f, 0, 300, newsCell.frame.size.height); 

}

但同样的问题在这里….任何想法,我想念?

编辑:此问题的另一个解决scheme是更改自定义单元的框架:

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

不知道试试这个希望,这有助于你

为此,首先您可以使用UIImageView来覆盖整个视图,并将其图像设置为边界图像。 现在在这个imageview上添加一个表格视图,使得这个图片的边界是可见的。

我认为你需要dynamic高度Tableviewcell而不是宽度。

UITableView的委托方法将对此有所帮助:

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

这将返回每个细胞的高度。 您可以按照以下示例代码来实现它。 这是基于dynamic文本内容显示dynamic高度。

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

你也可以试试这个 。

用这个:

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

UITableView委托方法并返回一个浮点值(CellHight + space bw cells)。