gpt4 book ai didi

ios - 改变 TableView 或 TableViewCell 的宽度

转载 作者:行者123 更新时间:2023-12-01 19:03:00 25 4
gpt4 key购买 nike

大家好 :) 我尝试更改单元格的宽度,因此单元格和表格 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com