gpt4 book ai didi

ios - 在 TableView Controller 中搜索时,与 UILabel 相比,动态创建的 UIButton 的屏幕闪烁

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

我有一个简单的 TableView Controller ,顶部有一个搜索栏。用户开始输入,如果没有找到结果,我会显示一个我以编程方式创建的 UIButton。

奇怪的是,如果我使用 UIButton,我键入每个字母时,屏幕都会刷新,您可以看到按钮的文本再次闪烁。它非常明显且不平滑。因此,用户输入一个字母并显示“单击此处创建”等。但是我输入的每个字母,该文本似乎都会重新呈现。

但是,如果我对 UILabel 执行完全相同的操作,它不会对每个键入的字母重新呈现并且非常流畅。

我使用按钮的原因是,如果用户输入的名称不存在,单击此按钮将为用户创建名称。我假设我不能为 UILabel 调用选择器,我可以吗?

这是我在 UIButton 中使用的代码:

viewDidLoad:

self.createButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//self.createButton = [[UIButton alloc] init];
self.createButton.frame = CGRectMake(-15, 90, 300, 30);
[self.createButton addTarget:self action:@selector(createButtonWasTapped:) forControlEvents:UIControlEventTouchUpInside];
self.createButton.backgroundColor = [UIColor clearColor];
self.createButton.showsTouchWhenHighlighted = YES;
self.createButton.tintColor = [UIColor whiteColor];
self.createButton.titleLabel.font = [UIFont systemFontOfSize:18.0];
//self.createButton.opaque = NO;
self.createButton.hidden = YES;
[self.view addSubview:self.createButton];

在下面的方法中,如果没有找到结果,我将动态创建按钮:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
self.nameAddSearchBar.showsCancelButton = YES;

_autocompleteFetchedResultsController = nil;
NSError *error;
if (![[self autocompleteFetchedResultsController] performFetch:&error])
{
// Handle Error
}
else
{
[self.nameAddSearchTableView reloadData];
[self.createButton setHidden:_autocompleteFetchedResultsController.fetchedObjects.count > 0];
[self.createButton setTitle:[NSString stringWithFormat:@"Click Here to Create %@", self.nameAddSearchBar.text] forState:UIControlStateNormal];
... more code
}

按钮的选择器正在调用:

- (void)createButtonWasTapped:(id)sender
{
self.selectedName = self.nameAddSearchBar.text;
[self.delegate nameTextFieldTableViewController:self didSelectCell:self.selectedName];
[self dismissViewControllerAnimated:YES completion:nil];

}

我找不到从 UILabel 调用选择器的方法,但如果可以,那将解决我的问题。如果我做不到,我需要理解为什么每次输入字母时,我的“按钮”都会重新呈现。

任何帮助都会很有帮助!谢谢,

最佳答案

这是一个非常简单的修复。

我将按钮类型从圆角矩形更改为自定义:

self.createButton = [UIButton buttonWithType:UIButtonTypeCustom];

..随着这个变化,闪烁停止了!奇怪,但对我来说完全没问题!

关于ios - 在 TableView Controller 中搜索时,与 UILabel 相比,动态创建的 UIButton 的屏幕闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22192080/

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