UIWeb视图从数组加载错误的地址

时间:2013-09-25 03:08:20

标签: ios xcode tableview detailview

我有一个应用程序填充表格并在选中单元格时推送Web视图。我有两个问题

  1. Web视图会推送并加载但加载的网址不正确。即第一个单元格应该加载一个网址,但它似乎是随机的。 Web地址来自阵列。

  2. 每次启动应用时,网页视图都不会推送选定的第一个单元格。

  3. 继承我的代码。任何帮助都会被提及。

    表格视图

    - (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;
    
    }
    

    详情视图

    - (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];
    }
    

2 个答案:

答案 0 :(得分:1)

[webAddress allKeys ];将返回NSArray但排序不在字典中维护,因此它返回随机数组。

答案 1 :(得分:0)

menuItems = [webAddress allKeys];

用排序数组或

替换上面的代码
menuItems = @[@"one", @"two",...];