除了调用indexPath以外,通过其他方法以编程方式在tableView中选择行 [英] Select row in tableView programmatically by other method beside calling their indexPath

查看:42
本文介绍了除了调用indexPath以外,通过其他方法以编程方式在tableView中选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I call row in tableView by using other method beside using [NSIndexPath indexPathForRow:0 inSection:0] method?

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

For example, every row in tableView has their own "action key" that detect from XML. So, i want to select the row in tableVIew programmatically by using this "action key" instead of using indexPath method.

我已经在SO上进行搜索,但是所有方法都在调用他们的indexPath。
有人有什么主意吗?

I already search on SO but all the method are calling their indexPath. Anybody have any idea?

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

These are the method that I currently used for my app to select row in 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"] );
    }
}


推荐答案

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

When dealing with a UITableView there's finally no advisable way to access or interact with cells without using NSIndexPath. So I'd recommend you to create a solid data source to determine the correct NSIndexPath for a single data/model object.

这篇关于除了调用indexPath以外,通过其他方法以编程方式在tableView中选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆