gpt4 book ai didi

ios - 使用自动布局的表格 View 单元格上的自定义附件按钮不清晰可见

转载 作者:行者123 更新时间:2023-11-30 14:12:28 26 4
gpt4 key购买 nike

我正在 UItableview 单元格上创建自定义附件图像。当我运行该应用程序时,这些附件图像被剪裁(请参阅第二个屏幕截图)。当我滚动表格 View 时,附件图像不再被剪切(请参见第一个屏幕截图)。

代码:

- (void)viewDidLoad {
[super viewDidLoad];

self.view.backgroundColor = [UIColor darkGrayColor];
MaintableView = [[UITableView alloc]init];
MaintableView.translatesAutoresizingMaskIntoConstraints = NO;
MaintableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
MaintableView.dataSource=self;
MaintableView.delegate=self;
MaintableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[MaintableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[myview addSubview:MaintableView];

NSDictionary * views = NSDictionaryOfVariableBindings(MaintableView);

NSArray * horizentalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[MaintableView]-5-|" options:0 metrics:nil views:views];

NSArray * verticalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[MaintableView]-50-|"options:0 metrics:nil views:views];

[myview addConstraints:horizentalConstraint];
[myview addConstraints:verticalConstraint];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return Mainarray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

UIView *sectionHeaderView = [[UIView alloc]init];
sectionHeaderView.backgroundColor = [UIColor clearColor];
return sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 5.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[MaintableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];

[delegate processCompleted:indexPath];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[MaintableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor blackColor];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *Cell = [MaintableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (Cell == nil)
{
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

UIView *bgview = [[UIView alloc] init];
bgview.backgroundColor = [bg colorWithHexString:@"27AE60"];
Cell.selectedBackgroundView = bgview;

accessoryView = accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0.jpg"]];
accessoryView.backgroundColor = [UIColor clearColor];
accessoryView.translatesAutoresizingMaskIntoConstraints = NO;
[Cell.contentView addSubview:accessoryView];

NSDictionary * views = NSDictionaryOfVariableBindings(accessoryView);

NSLayoutConstraint * constraint = [NSLayoutConstraint constraintWithItem:accessoryView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:Cell.contentView attribute:NSLayoutAttributeTop multiplier:1.0f constant:10.f];
[Cell.contentView addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:accessoryView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[Cell.contentView addConstraint:constraint];

constraint = [NSLayoutConstraint constraintWithItem:accessoryView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
[Cell.contentView addConstraint:constraint];

NSArray * horizentalConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[accessoryView]-10-|" options:0 metrics:nil views:views];

[Cell.contentView addConstraints:horizentalConstraint];


Cell.textLabel.text = [Mainarray objectAtIndex:indexPath.section];
Cell.textLabel.textColor = [UIColor blackColor];

UIFont *CiutadellaBold = [UIFont fontWithName:@"Bitter-Regular" size:17.0f];
[Cell.textLabel setFont:CiutadellaBold];

Cell.textLabel.numberOfLines = 2;
Cell.backgroundColor = [bg colorWithHexString:@"EAECEE"];

[Cell.layer setCornerRadius:7.0f];
[Cell.layer setMasksToBounds:YES];

return Cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
<小时/>

滚动后:表格 View 附件图像清晰地显示如下:

enter image description here

滚动之前:显示不清晰:

enter image description here

最佳答案

您遇到了 Cell.textLabelaccessoryView 之间的冲突。您应该使用 UITableViewCell 自己的 accessoryView,或者您应该创建标签和图像。

这是一个不需要所有这些约束的解决方案。我用你的代码测试了它;它有效:

Cell.accessoryView = ({
UIImage * image = [ViewController imageWithImage:[UIImage imageNamed:@"0.jpg"] scaledToSize:CGSizeMake(30, 30)];
UIView * icon = [[UIImageView alloc] initWithImage:image];
icon.backgroundColor = [UIColor clearColor];
icon;
});

或者,按照我的评论中的建议并在 Storyboard 中创建自定义 UITableViewCell。然后,您将能够控制 textLabelaccessoryView 重叠。

关于ios - 使用自动布局的表格 View 单元格上的自定义附件按钮不清晰可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31623341/

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