gpt4 book ai didi

ios - 来自另一个 UIVIewController 的 selectRowAtIndexPath 不起作用

转载 作者:行者123 更新时间:2023-12-01 18:14:53 24 4
gpt4 key购买 nike

我想调用 selectRowAtIndexPath 在作为 UIViewController 的 subview 的 UITableView 上,我从中调用它。

我已经设置了它,这样当你选择一个单元格时它会变成灰色,这很好,但是我正在将不同的数据集加载到 UITableView 中和从 UITableView 中加载出来,并且一旦进行了选择,我就会将选择的 NSIndexPath 发送回 UIViewController。然后,当 View 下一次加载正确的 NSIndexPath 数据集时,我从 UIViewController 调用此方法。

if (codeIndexPath != nil) {
[filterViewController.tableView selectRowAtIndexPath:codeIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}

然后在 UITableView 类中我的 cellForRowAtIndexPath didSelectRowAtIndexPath 看起来像这样。
- (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];
}
// Configure the cell...
NSString *projectDescriptionString = [currentFilterMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = projectDescriptionString;
if (indexPath == currentlySelectedIndex) {
cell.highlighted = YES;
} else if (indexPath == currentlySelectedIndex) {
cell.highlighted = NO;
}
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

cell.selectionStyle = UITableViewCellSelectionStyleGray;

// send this data back in the delegate so you can use it to show where the tick is again if you need too.
currentlySelectedIndex = indexPath;

[[self delegate] updateInstallTableWithFilter:[currentFilterMutableArray objectAtIndex:indexPath.row] FilterType:filterType InstallIndex:indexPath];

}

当它在屏幕上加载时,正确的单元格将突出显示一秒钟,然后变回白色。

更新

cellForRowAtIndexPath 中的新 if 语句
if ([indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = YES;
} else if (![indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = NO;
}

我仍然收到同样的错误。

最佳答案

UITableViewController 有一个名为 clearsSelectionOnViewWillAppear 的属性. From the doc :

When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.



所以在那个 TableView Controller 子类中,在 viewDidLoad ...
- (void)viewDidLoad {
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
}

关于ios - 来自另一个 UIVIewController 的 selectRowAtIndexPath 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23025120/

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