除了调用 indexPath 之外,还可以通过其他方法以编程方式在 tableView 中选择行

Posted

技术标签:

【中文标题】除了调用 indexPath 之外,还可以通过其他方法以编程方式在 tableView 中选择行【英文标题】:Select row in tableView programmatically by other method beside calling their indexPath 【发布时间】:2014-01-08 07:30:53 【问题描述】:

除了[NSIndexPath indexPathForRow:0 inSection:0]方法之外,如何使用其他方法调用tableView中的row?

例如,tableView 中的每一行都有自己的“操作键”,可以从 XML 中检测到。因此,我想通过使用此“操作键”而不是使用 indexPath 方法以编程方式选择 tableVIew 中的行。

我已经在 SO 上进行了搜索,但所有方法都在调用它们的 indexPath。 有人知道吗?

这些是我目前用于我的应用程序在 tableView 中选择行的方法:

-(void)viewDidAppear:(BOOL)animated;
    NSLog(@"viewDidAppear");
    [super viewDidAppear:YES];

    // saving an NSString
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *action_app = [defaults objectForKey:@"app"];
    [defaults setObject:@"closed_NewVC" forKey:@"closed_NewVC"];

    //will select row in tableView when receive key action from Push Notification
    if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a1"]) && ([AryStoreInboxItem count]>0))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a2"]) && ([AryStoreInboxItem count]>1))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a3"]) && ([AryStoreInboxItem count]>2))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a4"]) && ([AryStoreInboxItem count]>3))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a5"]) && ([AryStoreInboxItem count]>4))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:4 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a6"]) && ([AryStoreInboxItem count]>5))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:5 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a7"]) && ([AryStoreInboxItem count]>6))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:6 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"a8"]) && ([AryStoreInboxItem count]>7))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"d1"]) && ([AryStoreInboxItem count]>8))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:8 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"e1"]) && ([AryStoreInboxItem count]>9))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:9 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"m11"]) && ([AryStoreInboxItem count]>10))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:10 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"m12"]) && ([AryStoreInboxItem count]>11))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:11 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b1"]) && ([AryStoreInboxItem count]>12))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:12 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b2"]) && ([AryStoreInboxItem count]>13))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:13 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b3"]) && ([AryStoreInboxItem count]>14))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:14 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b4"]) && ([AryStoreInboxItem count]>15))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:15 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b5"]) && ([AryStoreInboxItem count]>16))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:16 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b6"]) && ([AryStoreInboxItem count]>17))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:17 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b7"]) && ([AryStoreInboxItem count]>18))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:18 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b8"]) && ([AryStoreInboxItem count]>19))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:19 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b9"]) && ([AryStoreInboxItem count]>20))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:20 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b10"]) && ([AryStoreInboxItem count]>21))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:21 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else if (([[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] isEqualToString:@"b11"]) && ([AryStoreInboxItem count]>22))

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:22 inSection:0];
        [self->tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        [self tableView:self->tableView didSelectRowAtIndexPath:indexPath];

    else

        NSLog(@"Others key sent from Push Notification OR key/value not exist in tableView since user not update");
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"app"];
        NSLog(@" key form push has been cleared! ==> %@ ! ",[[NSUserDefaults standardUserDefaults]objectForKey:@"app"] );
    

【问题讨论】:

为什么不使用 Switch case 而不是 if-else 语句??? @Reformer ..这不是我现在的问题..“app”键来自推送通知..我的问题是如何通过使用其他方法而不是我的方法来调用 tableView 中的行现在使用 【参考方案1】:

在处理UITableView 时,最终没有不使用NSIndexPath 的可取的方式来访问或与单元格交互。因此,我建议您创建一个可靠的数据源,以确定单个数据/模型对象的正确 NSIndexPath

【讨论】:

感谢您的解释...看来我已经浪费了一周的时间来解决这个问题..【参考方案2】:

您的数据源应该在您的操作键和索引路径之间进行转换。

也就是说,您的数据源可以接收您的 XML 类广播的通知。

【讨论】:

以上是关于除了调用 indexPath 之外,还可以通过其他方法以编程方式在 tableView 中选择行的主要内容,如果未能解决你的问题,请参考以下文章

除了 keycloak 令牌之外,我还需要其他任何东西来访问使用 keycloak 保护的服务吗?

Java 中除了 MDB 之外还都有哪些其他可能性来对消息消费者进行编程?

除了堆栈和堆模型之外,C++ 还实现了哪些其他形式的自动和动态存储? [复制]

除了其他自变量之外,还使用因变量来估算其中一个自变量的缺失值?

除了通过 main 的 argv 之外,还有其他方法可以将用户参数传递给程序吗?

除了[消息]之外,如何调用Slack API中的任何其他事件?