我有一個自定義的UItableView。我正在從API加載數據,我的主視圖是 我正在從API加載數據。 objective-c根據我的菜單發送url選擇
我的問題是,我有一個左側的菜單,我想發送一個URL,當用戶選擇任何菜單acoording選擇菜單視圖將加載。
- (空)的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
從該代碼我收到數據
視圖將 - (無效)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
我ContactsTableViewController viewDidLoad
- (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];
}
一些能告訴我怎樣才能解決我的問題...謝謝
那麼你實際上問的是如何讓網址進入'ContactsTableViewController'實例? – trojanfoe
@trojanfoe是的..請再次檢查我的代碼.i更新 –
添加'url'屬性到視圖控制器並設置它?在這種方法中有很多重複的代碼可以減少到10行。 – trojanfoe