gpt4 book ai didi

objective-c - 当单元格包含 UITextView 时,不会触发 UITableView willDisplayCell

转载 作者:行者123 更新时间:2023-11-29 13:30:32 24 4
gpt4 key购买 nike

我想我偶然发现了 UITableView 类中的奇怪错误。如果表中的单元格包含 UItextView(在我的情况下不可编辑且不可滚动),并且用户选择其文本(显示复制菜单),则包含该特定 UITextView 的单元格将不再正常工作。 UITableView 将不再为该单元格触发 willDisplayCell 和 cellForRowAtIndexPath 方法。我进行了一些测试,当单元格离开屏幕时,它们会从 visibleCells 数组中移除,当它们再次出现在屏幕上时,它们会放回 visibleCells 数组中,但不会调用委托(delegate)方法。

这很烦人,因为它会阻止您配置可重用单元格。我不确定这是否被认为是一个错误。我想请您仔细检查并确认此行为。

这是我简化的测试代码,所以你可以自己检查一下:

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

tableData = [[NSArray alloc]initWithObjects:@"First Text",@"Second",@"Third",@"Fourth",@"5th",@"6th",@"7th",@"8th",@"9th",@"10th",@"11th",@"12th",@"13th", nil];

self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
self.tableView.backgroundColor = [UIColor darkGrayColor];
self.tableView.dataSource = self;
self.tableView.delegate = self;

[self.view addSubview:self.tableView];

UIButton* btn = [[UIButton alloc]initWithFrame:CGRectMake(200, 0, 100, 30)];
[btn setBackgroundColor:[UIColor redColor]];
[btn setTitle:@"Clickme" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(onBtnClick:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];
// Do any additional setup after loading the view, typically from a nib.
}


#pragma mark - TableView Datasource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return tableData.count;
}

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


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UITextView* tv = [[UITextView alloc]initWithFrame:cell.contentView.bounds];
[tv setEditable:NO];
[cell.contentView addSubview:tv];
}

for (UIView* subview in cell.contentView.subviews)
{
if([subview isKindOfClass:[UITextView class]])
{
UITextView* tv = (UITextView*)subview;
tv.text = [tableData objectAtIndex:indexPath.row];
}
}
NSLog(@"Configured cell at index: %d", indexPath.row);
return cell;

}

#pragma mark - TableView Delegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Will display cell at index: %d", indexPath.row);
}


#pragma mark - Button callback handlers
-(void)onBtnClick:(id)sender
{
NSLog(@"Number of visible cells: %d",[self.tableView visibleCells].count);
}

通过运行此代码,您可以看到在第一个单元格中选择文本将导致 tableview 在单元格再次出现在屏幕上时停止触发委托(delegate)事件。通过按下按钮,您可以检查可见单元格的数量是否始终正确,这意味着单元格正在添加和删除到可见单元格列表中。只是未触发的回调委托(delegate)。

最佳答案

不完全是解决方案,但替代方案是子类化 UITableViewCell 并覆盖 - (void)prepareForReuse 以在重用之前进行任何清理。

关于objective-c - 当单元格包含 UITextView 时,不会触发 UITableView willDisplayCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12019831/

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