Ios 在UITableView中选择第一行作为默认行
我有一个基于视图的应用程序,我正在将tableview作为子视图添加到主视图中。我已使用UITableViewDelegate来响应表方法。一切正常,但我想选择第一行或UITableView作为默认selectedHighlightedIos 在UITableView中选择第一行作为默认行,ios,uitableview,uikit,Ios,Uitableview,Uikit,我有一个基于视图的应用程序,我正在将tableview作为子视图添加到主视图中。我已使用UITableViewDelegate来响应表方法。一切正常,但我想选择第一行或UITableView作为默认selectedHighlighted 请帮助我,我需要什么代码以及我需要把它放在哪里。我们根据单元格是否为第一个单元格使用自定义背景图像。。。中间单元格或最后一个单元格。这样我们就可以看到整张桌子的圆角。当选择行时,它会交换一个漂亮的“突出显示”单元格,以向用户反馈他们已经选择了一个单元格 UIIm
请帮助我,我需要什么代码以及我需要把它放在哪里。我们根据单元格是否为第一个单元格使用自定义背景图像。。。中间单元格或最后一个单元格。这样我们就可以看到整张桌子的圆角。当选择行时,它会交换一个漂亮的“突出显示”单元格,以向用户反馈他们已经选择了一个单元格
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
}
else if (row == 0)
{
rowBackground = [UIImage imageNamed:@"topRow.png"];
selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
}
else if (row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:@"bottomRow.png"];
selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
}
else
{
rowBackground = [UIImage imageNamed:@"middleRow.png"];
selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
如果希望,只需将第一个单元格(位于indexPath.row==0)设置为使用自定义背景
这源于马特·加拉赫的你可以这样做:
- (void)viewDidLoad {
[super viewDidLoad];
NSIndexPath *ip=[NSIndexPath indexPathForRow:0 inSection:0];
[myTableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
在代码中使用此选项的最佳方法是,如果您想默认选择任何行,请在ViewDidDisplay中使用。以下是如何在Swift 1.2中执行此操作:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// assuming you had the table view wired to IBOutlet myTableView
// and that you wanted to select the first item in the first section
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:0];
}
override func viewWillAppear(animated: Bool) {
let firstIndexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.selectRowAtIndexPath(firstIndexPath, animated: true, scrollPosition: .Top)
}
要在第一次加载表时仅选择第一个单元格,您可能会认为使用viewDidLoad是正确的选择,但是,在执行时,表尚未加载其内容,因此它将无法工作,并且可能会使应用程序崩溃,因为NSIndexPath将指向一个不存在的单元格 解决方法是使用一个变量,该变量指示表之前已加载,并相应地执行工作
@implementation MyClass {
BOOL _tableHasBeenShownAtLeastOnce;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_tableHasBeenShownAtLeastOnce = NO; // Only on first run
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ( ! _tableHasBeenShownAtLeastOnce )
{
_tableHasBeenShownAtLeastOnce = YES;
BOOL animationEnabledForInitialFirstRowSelect = YES; // Whether to animate the selection of the first row or not... in viewDidAppear:, it should be YES (to "smooth" it). If you use this same technique in viewWillAppear: then "YES" has no point, since the view hasn't appeared yet.
NSIndexPath *indexPathForFirstRow = [NSIndexPath indexPathForRow:0 inSection: 0];
[self.tableView selectRowAtIndexPath:indexPathForFirstRow animated:animationEnabledForInitialFirstRowSelect scrollPosition:UITableViewScrollPositionTop];
}
}
/* More Objective-C... */
@end
Swit 3.0更新解决方案
以下是我的swift 3.0解决方案:
var selectedDefaultIndexPath=false
覆盖func VIEWDID显示动画:Bool{
super.ViewDidApparanimated
如果dataSource.isEmpty==false,则selectedDefaultIndexPath==false{
设indexPath=IndexPathrow:0,节:0
//如果没有,cell.backgroundView将为零。
tableView.selectRowat:indexPath,动画:false,滚动位置:。无
//触发委托做某事。
_=tableView.delegate?.tableView?tableView,将选择rowat:indexPath
selectedDefaultIndexPath=true
}
}
func tableView_utableview:UITableView,将在indepath:indepath->indepath中选择行?{
让cell=tableView.cellForRowat:indexPath
单元格?.selectedBackgroundView?.backgroundColor=UIColorhexString:F0
返回索引XPath
}
Swift 4更新:
如果要选择其他节中的任何其他行,请更改行和节值。Swift 5.x更新:
这是我的工作,希望这将有助于你
快乐编码:我也在尝试如何做到这一点。它需要在调用UITableViewDataSource函数后设置,以便表视图中包含某些内容。UITableView似乎没有任何在加载数据后触发的回调。您可以按照正确的答案进行解释。您还需要将所选索引存储在某个位置,因此在滚动表视图时,它会按照您的意愿运行,这是因为在tableview生成其单元格之前调用了viewDidLoad。使用performSelector:onObject:afterDelay:在加载后调用它,或者按照其他地方的建议尝试ViewDidDisplay:。检查上面的答案。切勿在viewDidLoad中使用此函数:在ViewDidDisplay中调用此函数可获得良好的结果。由于某些奇怪的原因,我的单元格不会在ViewDidDisplay中选择此函数,但它在ViewWillAppEarth中工作正常。如果出现某些筛选器,并导致空数组,则应用程序可能会崩溃。需要在开始时添加一个检查我使用这种方法首先选择一个单元格,然后我调用代理,但是使用SelectRowatineXpath选择的单元格永远不会被取消选择。不允许多次选择。有什么帮助吗?请,谢谢。手机永远不会被取消选择,甚至打电话取消选择。在ViewDidDisplay而不是viewDidLoad上调用selectRowAtIndexPath。抱歉,谢谢代表的电话肯定解决了我的问题,没有创建selectedBackgroundView。我必须修改willSelect以重新创建视图。重写func tableView\utableview:UITableView,将在indexPath:indexPath->indexPath中选择行?{let cell=tableView.cellForRowat:indexath let bgColorView=UIView bgColorView.backgroundColor=colorLiteralred:1,green:0.596,blue:0.004,alpha:1单元格?.selectedBackgroundView=bgColorView返回indexath}
override func viewWillAppear(animated: Bool) {
let firstIndexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.selectRowAtIndexPath(firstIndexPath, animated: true, scrollPosition: .Top)
}
@implementation MyClass {
BOOL _tableHasBeenShownAtLeastOnce;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_tableHasBeenShownAtLeastOnce = NO; // Only on first run
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ( ! _tableHasBeenShownAtLeastOnce )
{
_tableHasBeenShownAtLeastOnce = YES;
BOOL animationEnabledForInitialFirstRowSelect = YES; // Whether to animate the selection of the first row or not... in viewDidAppear:, it should be YES (to "smooth" it). If you use this same technique in viewWillAppear: then "YES" has no point, since the view hasn't appeared yet.
NSIndexPath *indexPathForFirstRow = [NSIndexPath indexPathForRow:0 inSection: 0];
[self.tableView selectRowAtIndexPath:indexPathForFirstRow animated:animationEnabledForInitialFirstRowSelect scrollPosition:UITableViewScrollPositionTop];
}
}
/* More Objective-C... */
@end
let indexPath = IndexPath(row: 0, section: 0)
tblView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let indexPath = IndexPath(row: 0, section: 0)
myTableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let indexPath = IndexPath(row: 0, section: 0)
tableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
tableView.delegate?.tableView?(tableView, didSelectRowAt: indexPath)
}