selectRowAtIndexPath from another UIVIewController

2019-08-09 18:39发布

I am trying to call selectRowAtIndexPath on a UITableView that is a subview to the UIViewController I am calling it from.

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];
}

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.

Update

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.

1条回答
再贱就再见
2楼-- · 2019-08-09 18:46

UITableViewController has a property called 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.

So in that table view controller subclass, in viewDidLoad...

- (void)viewDidLoad {
    [super viewDidLoad];
    self.clearsSelectionOnViewWillAppear = NO;
}
查看更多
登录 后发表回答