【问题标题】:UIWeb view loads wrong address from arrayUIWeb 视图从数组中加载错误的地址
【发布时间】:2013-09-30 11:47:21
【问题描述】:

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

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

  2. 每次启动应用时,Web 视图都不会推送到选择的第一个单元格上。

这是我的代码。任何帮助都将不胜感激。

表格视图

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

【问题讨论】:

    标签: ios xcode tableview detailview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      menuItems = [webAddress allKeys];
      

      将上面的代码替换为有序数组或

      menuItems = @[@"one", @"two",...];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多