问题描述
我正在尝试在 UITableView 上调用 selectRowAtIndexPath,该 UITableView 是我从中调用它的 UIViewController 的子视图.
I am trying to call selectRowAtIndexPath on a UITableView that is a subview to the UIViewController I am calling it from.
我已经设置好了,当你选择一个单元格时它会变灰,这很好,但是我正在加载不同的数据集进出 UITableView,每当做出选择时,我都会将所选的 NSIndexPath 发送回UIViewController.然后,当接下来为 NSIndexPath 加载正确的数据集时,我会从 UIViewController 调用此方法.
I have set it up so that when you select a cell it goes grey and thats fine, however I am loading different data sets in and out of the UITableView and when ever a selection is made I am sending that selected NSIndexPath back to the UIViewController. Then when the view is next loaded with the correct data set for the NSIndexPath I call this method from my UIViewController.
if (codeIndexPath != nil) {
[filterViewController.tableView selectRowAtIndexPath:codeIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
}
然后在 UITableView 类中,我的 cellForRowAtIndexPath 和 didSelectRowAtIndexPath 看起来像这样.
Then in the UITableView class my cellForRowAtIndexPath and didSelectRowAtIndexPath look like this.
- (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];
}
当它加载到屏幕上时,正确的单元格会高亮显示一秒钟,然后变回白色.
When It loads on the screen the correct cell will highlight for a second then go back to white.
cellForRowAtIndexPath 中的新 if 语句
New if statment inside cellForRowAtIndexPath
if ([indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = YES;
} else if (![indexPath isEqual:currentlySelectedIndex]) {
cell.highlighted = NO;
}
我仍然收到同样的错误.
I am still receiving the same error.
推荐答案
UITableViewController 有一个名为 clearsSelectionOnViewWillAppear
的属性.来自文档:
UITableViewController has a property called clearsSelectionOnViewWillAppear
. From the doc:
当table view第一次被加载时,表视图控制器重新加载表视图的数据.它也清除它的选择(有或没有动画,取决于请求)每次显示表格视图时.UITableViewController类在超类方法 viewWillAppear: 中实现了这一点.你可以通过更改中的值来禁用此行为clearsSelectionOnViewWillAppear 属性.
所以在那个表视图控制器子类中,在 viewDidLoad...
So in that table view controller subclass, in viewDidLoad...
- (void)viewDidLoad {
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
}
这篇关于来自另一个 UIVIewController 的 selectRowAtIndexPath 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!