Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

ios - Select row in tableView programmatically by other method beside calling their indexPath

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"] );
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...