Select row in tableView programmatically by other

2019-09-19 23:40发布

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

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.

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

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"] );
    }
}

2条回答
女痞
2楼-- · 2019-09-19 23:47

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-09-19 23:50

Your datasource should translate between your action key and the index path.

Ie your data source could tune in for notification broadcasted by your XML classes.

查看更多
登录 后发表回答