简体   繁体   English

UITableView中最后选择的行

[英]Last selected Row in UITableView

I have UITableView, in which if any row is selected i am storing some values. 我有UITableView,如果选择任何行,我会存储一些值。 If any other row is selected i am displaying an alertView with warning that his previous selection will be gone. 如果选择了任何其他行,我将显示一个alertView,警告他之前的选择将会消失。 This alerView has 2 buttons YES and NO. 这个alerView有2个按钮YES和NO。 If YES is selected go on to selecting the new row which is fine. 如果选择是,则继续选择新行即可。 But if no is selected i want reselect the previous row. 但如果没有被选中,我想重新选择前一行。 I can post the code if asked. 如果被问到,我可以发布代码。 But it seems more of logical question than code oriented. 但它似乎比面向代码更具逻辑性。

Thank you. 谢谢。

first store the clicked tableview index to your global variable in UITableView Delegate like below, 首先将点击的tableview索引存储到UITableView Delegate全局变量,如下所示,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView==yourTableview) {
        yourMemberVariable=[indexPath row];
  }
}

then check yourMemberVariable in UIAlertView delegate 然后在UIAlertView delegate检查yourMemberVariable

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 0)//OK button pressed
    {
       //do something
    }else{
    if(yourMemberVariable>someValue){ //perform your own conditions whatever
       [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:yourMemberVariable   inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
       }
    }
 }

You want to know the last row selected ,you can do this by setting 您想知道所选的最后一行,您可以通过设置来完成

    tableView.remembersLastFocusedIndexPath = true  

in viewDidLoad function. 在viewDidLoad函数中。

and then calling 然后打电话

   tableView.indexPathForSelectedRow 

wherever required. 在需要的地方。

It's simple. 这很简单。 Just save previous row into class member. 只需将上一行保存到类成员中。 Good luck! 祝好运!

Keep track of the last selected item. 跟踪最后选择的项目。 On the select delegate for the table, store what was selected in a variable accessible througout the class. 在表的select委托中,将选择的内容存储在可通过类访问的变量中。

NSInteger selectedRow = 0;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedRow = [indexPath row];
}

Now you know what element to select if NO is pressed in the UIAlertView delegate 现在您知道在UIAlertView委托中按下NO时要选择的元素

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

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

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