xcode 自动单元格选中 UITableView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7667151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:04:33  来源:igfitidea点击:

Automatically cell selected UITableView

iosxcodeuitableview

提问by Mc.Lover

My UITableView opens via PopOverViewController , so How can I load one of these cells automatically after app did load ,

我的 UITableView 通过 PopOverViewController 打开,所以如何在应用加载后自动加载这些单元格之一,

the cell selecting process on MainViewController

MainViewController 上的单元格选择过程

- (void)setDetailItem:(id)newDetailItem {

    if (detailItem != newDetailItem) {
        [detailItem release];
        detailItem = [newDetailItem retain];

        //---update the view---
        label.text = [detailItem description];
    }

}

and cell selecting in TableViewController :

和 TableViewController 中的单元格选择:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    myAppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate];
    appDelegate.viewController.detailItem = [list objectAtIndex:indexPath.row];  

}

I use this code in TableViewController but does not work ! It means after press the the popOver button the code just highlight the cell !!

我在 TableViewController 中使用此代码但不起作用!这意味着按下 popOver 按钮后,代码只会突出显示单元格!

 [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];

I used above code in different methods like viewDidAppear, viewWillAppearand didSelectRowAtIndexPathand ...

我在不同的方法,比如使用上面的代码viewDidAppearviewWillAppeardidSelectRowAtIndexPath与...

Thank you

谢谢

回答by albertamg

When you call selectRowAtIndexPath:animated:scrollPosition:, tableView:didSelectRowAtIndexPath:is notcalled on the delegate.

当你打电话selectRowAtIndexPath:animated:scrollPosition:tableView:didSelectRowAtIndexPath:不是所谓的委托。

From the selectRowAtIndexPath:animated:scrollPosition:reference:

selectRowAtIndexPath:animated:scrollPosition:参考:

Calling this method does not cause the delegate to receive a tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath: message, nor will it send UITableViewSelectionDidChangeNotification notifications to observers.

调用此方法不会导致委托收到 tableView:willSelectRowAtIndexPath: 或 tableView:didSelectRowAtIndexPath: 消息,也不会向观察者发送 UITableViewSelectionDidChangeNotification 通知。

So, instead of just calling selectRowAtIndexPath:animated:scrollPosition::

所以,而不是仅仅调用selectRowAtIndexPath:animated:scrollPosition:

 [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];

you could call the delegate methods manually:

您可以手动调用委托方法:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

if ([myTableView.delegate respondsToSelector:@selector(tableView:willSelectRowAtIndexPath:)]) {
    [myTableView.delegate tableView:self.tableView willSelectRowAtIndexPath:indexPath];
}

[myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition: UITableViewScrollPositionNone];    

if ([myTableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
    [myTableView.delegate tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}