简体   繁体   English

更改表格视图的选择

[英]Change the selection of a table view

I'm starting with iOS development and I want to implement the following behavior in a table view: Element 1 of the list cant be selected and when other row is selected the previous element in the list becomes the selected one. 我从iOS开发开始,我想在表视图中实现以下行为:无法选择列表的元素1,并且当选择了其他行时,列表中的上一个元素将成为选定的元素。 I know is senseless, it is just as an exercise. 我知道这是毫无意义的,它只是一种练习。 By the way my code is: 顺便说一下,我的代码是:

-(NSIndexPath *) tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row =[indexPath row];
if (row==0){
    return nil;
}
   return indexPath;
}

It works for avoiding the row 0 from being selected, now I need change the selected element to the previous when the row!=0. 它可以避免选择第0行,现在,当row!= 0时,我需要将所选元素更改为上一个元素。 How can I do it? 我该怎么做?

UITableView has a method named selectRowAtIndexPath:animated:scrollPosition: (documentation linked for you) where you can select a different row. UITableView有一个名为selectRowAtIndexPath:animated:scrollPosition:为您链接的文档)的方法,您可以在其中选择另一行。

A couple caveats about this though: you have to select a row via an NSIndexPath (not too hard to set up, once you get the hang of it) and your willSelectRowAtIndexPath may be called a second time when you programatically call selectRowAtIndexPath so you need to figure out a way to ignore that call while paying attention to the ones the user does with their finger. 不过,有几点警告:您必须通过NSIndexPath选择一行(一旦掌握了它,就很难设置),并且当您以编程方式调用selectRowAtIndexPath时,您的willSelectRowAtIndexPath可能会被第二次调用。找出忽略呼叫的方法,同时注意用户用手指所做的呼叫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM