سؤال

I have an app that populates a table and pushes a web view when cell in selected. I have two problems

  1. The web view pushs and loads however the web address that loads is incorrect. i.e first cell should load web address one but it appears to be random. The web addresses come from an array.

  2. The web view does not push on the first cell selected every time the app is launched.

Heres my code. Any help would be appriecated.

Table view

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad {

[self setupArray];
[super viewDidLoad];
// Do any additional setup after loading the view.
}

-(void)setupArray {
webAddress = [[NSMutableDictionary alloc]init];
[webAddress setObject:@"http://www.google.com.au" forKey:@"one"];
[webAddress setObject:@"http://www.finddrinkdine.com.au" forKey:@"Two"];
[webAddress setObject:@"http://www.kingsgroversl.com.au" forKey:@""three"];
[webAddress setObject:@"http://www.yahoo.com.au" forKey:@"Four"];
[webAddress setObject:@"http://www.scu.edu.au" forKey:@"Five"];
[webAddress setObject:@"http://www.whitenow.com.au" forKey:@"Six"];

menuItems = [webAddress allKeys];

}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [webAddress count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [menuItems objectAtIndex:indexPath.row];
    return cell;

}

Detail View

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return self;
}

-(void)viewWillAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:webAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[viewWeb loadRequest:req];

[super viewWillAppear:animated];
}
هل كانت مفيدة؟

المحلول

[webAddress allKeys]; will return NSArray but ordering is not maintained in Dictionaries and hence it is returning random array.

نصائح أخرى

menuItems = [webAddress allKeys];

replace above codes with sorted array or

menuItems = @[@"one", @"two",...];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top