1

我有一个自定义 UItableView 。我正在从 API 加载数据,我的主要观点是我正在从 API 加载数据。 在此处输入图像描述

我的问题是,我有一个左侧菜单,当用户选择任何与选择菜单视图相关的菜单时,我想发送一个 url。 在此处输入图像描述

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{

if(indexPath.row ==0){
    appdataModel.newsApiUrl = homePagesUrl;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.revealViewController revealToggleAnimated:YES];

    ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:@"ContactsTableViewController" bundle:nil];
    [self.navigationController pushViewController:contView animated:YES];

}else if (indexPath.row ==1){
    appdataModel.newsApiUrl = jatioNews;

    NSLog(@"here  1 :%@",appdataModel.newsApiUrl);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.revealViewController revealToggleAnimated:YES];
    ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:@"ContactsTableViewController" bundle:nil];
    [self.navigationController pushViewController:contView animated:YES];
}
else if (indexPath.row ==2){
    appdataModel.newsApiUrl = jatioNews;

    NSLog(@"here  1 :%@",appdataModel.newsApiUrl);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.revealViewController revealToggleAnimated:YES];
        ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:@"ContactsTableViewController" bundle:nil];
    [self.navigationController pushViewController:contView animated:YES];
}

}

#define homePagesNews @"http://198.72.115.125/~pratidin/api/topnews"
#define jatioNews @"http://198.72.115.125/~pratidin/api/categorynews/4"

这是我的 api 链接。如果用户选择主菜单,则视图将加载表单homePagesNews API,否则用户选择第二个菜单,则视图将从jatioNews API

从这段代码我得到数据

-(void)GetHomePageData{

NSString *urlString   = [NSString stringWithFormat:@"%@",url];
NSURL *url            = [[NSURL alloc]initWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response;
NSData *GETReply      = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
res = [NSJSONSerialization JSONObjectWithData:GETReply options:NSJSONReadingMutableLeaves|| NSJSONReadingMutableContainers error:nil];

}

现在我想当用户选择菜单时,可能会根据用户选择url链接更改主菜单或第二菜单或第三菜单,并且视图将加载到我的ContactsTableViewController

我的 ContactsTableViewControllerviewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    appdataModel = [AppDataModel getInstance]; 
    appdataModel.newsApiUrl = homePagesUrl;

     /**** for left side menu ***/

    SWRevealViewController *revealViewController = self.revealViewController;
    if ( revealViewController )
    {
        [self.sideBarButton setTarget: self.revealViewController];
        [self.sideBarButton setAction: @selector( revealToggle: )];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    }

     /**** for Contractview***/
    self.view.backgroundColor = [UIColor whiteColor];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];
    contactsArray  = [NSArray arrayWithContentsOfFile :path];
    [self GetHomePageData];

    [self.newsDataTableView reloadData];

}

有人可以告诉我如何解决我的问题...谢谢

4

1 回答 1

1

url属性添加到ContactsTableViewController

@interface ContactsTableViewController : UIViewController
@property NSURL *url;
...
@end

在它的viewDidLoad方法中,您可以使用您正在使用的任何方法加载数据:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession]
  dataTaskWithURL:self.url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        // Whatever
    }];

然后从 tableview 委托方法设置 URL:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{

    if (indexPath.row ==0){
        appdataModel.newsApiUrl = homePagesUrl;
    } else {
        appdataModel.newsApiUrl = jatioNews;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.revealViewController revealToggleAnimated:YES];

    ContactsTableViewController *vc = [[ContactsTableViewController alloc] initWithNibName:@"ContactsTableViewController" bundle:nil];
    vc.url = [NSURL URLWithString:appdataModel.newsApiUrl];
    [self.navigationController pushViewController:vc animated:YES];
}
于 2015-11-04T18:22:36.880 回答