uitableview页脚中的超链接

时间:2013-06-19 13:36:55

标签: ios uitableview uiwebview hyperlink footer

我正在使用InAppSettingsKit进行设置,并希望在表格视图页脚中添加公司超链接。我已经成功地在节标题中添加了一个uiwebview(它位于表格视图的底部并显示为页脚)。 webview显示超链接但不响应触摸。

Web视图在settingsViewController中创建:tableView:viewForHeaderForSection:

    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor clearColor];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(30, 0, self.view.frame.size.width-60, 35)];
    webView.delegate = self;
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:[NSDate date]];
    NSUInteger fontSize = 13;

    NSString *link = @"<a href=\"http://www.google.com\">My Hyperlink</a>";
    [webView loadHTMLString:[NSString stringWithFormat:@"<html><head></head><body style=\"font-family: sans-serif;font-size:%dpx;\"> <center>Copyright © %d %@</center></body></html>", fontSize, dateComponents.year,link]  baseURL:nil];
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    [containerView addSubview:webView];
    return containerView;

和uiwebview委托方法:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
DDLogVerbose(@"Web view should start load with request %@. InType: %d", inRequest.URL.absoluteString, inType);
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
    [[UIApplication sharedApplication] openURL:[inRequest URL]];
    return NO;
}

return YES;

}

谁能告诉我为什么触摸不起作用? 顺便说一句... webView:shoudStartLoadWithRequest:当表视图被加载/显示时立即被调用

THX!

3 个答案:

答案 0 :(得分:1)

在你的代码中我发现容器视图有两个问题,没有框架,你没有为你的webview设置适当的框架一旦尝试这个例子,你就会得到错误的地方。

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(30, 0, 100, 35)];

答案 1 :(得分:0)

原因是IASKSettingsDelegate方法settingsViewController:tableView:heightForHeaderForSection:为该特定部分返回0。返回与uiwebview高度相同的高度解决了这个问题。

答案 2 :(得分:0)

作为替代选择:从iOS 7开始,UIKit / NSAttributedString支持NSLinkAttributeName,甚至可以进行HTML的基本导入。 因此,您可以使用UITextView而不是UIWebView 作为表格视图页眉/页脚。

要导入HTML,请使用带有-[NSAttributedString data:options:documentAttributes:]选项的NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType

NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:NULL error:NULL];

UITextView *textView = [[UILabel alloc] initWithFrame: ...];
textView.editable = NO;
textView.scrollEnabled = NO;
textView.textContainerInset = UIEdgeInsetsZero; // optional
textView.attributedText = attributedText;

[UILabel显示超链接,但它们无法点击,请参阅UILabel and NSLinkAttributeName: Link is not clickable