0

我添加了一个自定义单元格。每当有人按下添加按钮时,就会弹出一个警报视图,他们会添加一个网站的名称。一旦按下保存,它就会以他们选择的任何名称保存在表格视图单元格中。然后,一旦有人单击该单元格,我希望它在我的 Web 视图中使用 http:// 加载。唯一的问题是它没有加载。我究竟做错了什么?

这是我的表格视图控制器

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"cellTwo";
    UITableViewCell *cell = [myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    if (indexPath.section == 0) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@",[tableData objectAtIndex:indexPath.row]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Index Selected,%d",indexPath.row);
    MainTwoViewController *View = [self.storyboard instantiateViewControllerWithIdentifier:@"mainTwo"];

    View.urlString = [[NSString alloc] initWithFormat:@"http://www.%@",tableData];
    View.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
}        

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //Only do the following action if the user hits the ok button
    if (buttonIndex == 1) {
        NSString *tapTextField = [alertView textFieldAtIndex:0].text;
        if (!tableData) {
            tableData = [[NSMutableArray alloc]init];
        }
        [tableData insertObject:tapTextField atIndex:0];
        [myTableView reloadData];
}

当然,我把它放在 mainTwo 视图控制器中,以便它加载 urlString。

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
4

3 回答 3

0

您实例化一个视图控制器但不显示它。

改用故事板 segues 并在prepareForSegue.

此外,请检查以下内容:
如果您使用情节提要,很可能您不必在cellForRowAtIndexPath.

另外,简化: -->
[NSString stringWithFormat@"%", string];
string

最后,在加载请求之前检查到达新视图控制器的 url。确保网址正确。实施UIWebViewDelegate检查加载失败或意外返回数据的方法。

于 2013-08-23T10:11:06.150 回答
0

我认为你的这段代码

View.urlString = [[NSString alloc] initWithFormat:@"http://www.%@",tableData];

应该像

View.urlString = [[NSString alloc] initWithFormat:@"http://www.%@",[tableData objectAtIndex:indexPath.row]];

在您的 didSelectRowAtIndexPath 方法中,还建议您使用 NSLog 检查您丢失数据的时间点

于 2013-08-23T11:18:37.547 回答
-1

好的,我刚刚发现了你的问题。

您应该首先在自定义单元格标签上放置一个点击手势。

UITapGestureRecognizer *openURLTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openURLTapped:)];

keypadDisableTapGestureRecognizer.numberOfTapsRequired = 1;
keypadDisableTapGestureRecognizer.numberOfTouchesRequired = 1;

[cell.textLabel addGestureRecognizer:openURLTapGestureRecognizer];

现在,在 this 中openURLTapped,您可以传递 url 并打开它。

-(void)openURLTapped:(UITapGestureRecognizer*)tap
{
    // your code here
}

它肯定会解决你的问题。

于 2013-08-23T10:08:48.790 回答