gpt4 book ai didi

ios - 检索带有边距的 UITableViewCell 的框架

转载 作者:行者123 更新时间:2023-11-29 02:23:06 25 4
gpt4 key购买 nike

我正在尝试创建一个可编辑的 UITableViewCell,它可以工作,但我缺少边距:

enter image description here

文本直接向左对齐,没有边距。所以我猜 cell.frame 部分有点错误。

但我不知道还能用什么,我尝试了 cell.contentView.frame,但没有任何效果:

UITextField *textField = [[UITextField alloc] initWithFrame:cell.frame];

textField.placeholder = @"What's your email address?";
textField.clearButtonMode = UITextFieldViewModeNever;
textField.textAlignment = NSTextAlignmentLeft;

[cell.contentView addSubview: textField];

return cell;

P.S 我稍后可能会添加一个图标,我也不能使用静态边距,它必须以某种方式计算

最佳答案

这应该向您展示如何以快速而肮脏的方式实现这一目标。只需将它放在现有的相同命名方法的位置,看看发生了什么,然后重构它

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

static NSString *CellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];

if (cell == nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectZero];

textField.placeholder = @"What's your email address?";
textField.clearButtonMode = UITextFieldViewModeNever;
textField.textAlignment = NSTextAlignmentLeft;
textField.text = @"hi";

if ([textField respondsToSelector:@selector(setTranslatesAutoresizingMaskIntoConstraints:)]) {
[textField setTranslatesAutoresizingMaskIntoConstraints: NO];
}

[cell.contentView addSubview: textField];

NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(textField);

[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[textField]-10-|" options:0 metrics: 0 views:viewsDictionary]];

[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=1)-[textField(==20)]-(>=1)-|" options:0 metrics: 0 views:viewsDictionary]];

[cell addConstraint:
[NSLayoutConstraint constraintWithItem:textField
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:cell
attribute:NSLayoutAttributeCenterY
multiplier:1
constant:0]];

textField.backgroundColor = [UIColor grayColor];

}

return cell;

}

关于ios - 检索带有边距的 UITableViewCell 的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27886355/

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