【问题标题】:UIWebView In A Custom TableView Cell自定义 TableView 单元格中的 UIWebView
【发布时间】:2012-07-12 20:40:13
【问题描述】:

我有一个简单的 Interface Builder 文件,它是一个自定义 TableView 单元。自定义 tableview 单元格有两个插座...

  1. 网页视图
  2. 文字标签

由于某些奇怪的原因,我收到此错误...

2012-07-12 16:28:23.206 VideoPush[3761:707] -[UITableViewCell webView]: unrecognized selector sent to instance 0x168cc0
2012-07-12 16:28:23.211 VideoPush[3761:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell webView]: unrecognized selector sent to instance 0x168cc0'
*** First throw call stack:
(0x355e188f 0x37988259 0x355e4a9b 0x355e3915 0x3553e650 0x87279 0x33075efb 0x33074fd9 0x33074763 0x33018f37 0x355401fb 0x32410aa5 0x324106bd 0x32414843 0x3241457f 0x3243c911 0x3243c8e3 0x3305a10f 0x33047b33 0x33015ac3 0x33015567 0x33014f3b 0x371d422b 0x355b5523 0x355b54c5 0x355b4313 0x355374a5 0x3553736d 0x3304686b 0x33043cd5 0x86e5b 0x86e00)
terminate called throwing an exception(lldb) 

我的 XIB

这里是CustomCell.h上面xib的控制器...

#import <UIKit/UIKit.h>

@interface VPCustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;

@end

最后是我的视图控制器,它包含实际的表格视图...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSString *htmlString = @"<html><head> <meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 120\"/></head> <body style=\"background:#FFF;margin-top:0px;margin-left:0px\"> <div><object width=\"120\" height=\"80\"> <param name=\"movie\" value=\"http://www.youtube.com/v/1Xqn5IHbusA&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param> <param name=\"wmode\" value=\"transparent\"></param> <embed src=\"http://www.youtube.com/v/1Xqn5IHbusA&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"120\" height=\"80\"></embed> </object></div></body></html>";
    // Configure the cell...
    [cell.webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];
    cell.titleLabel.text = @"Hello";

    return cell;
}

注意:在接口文件中我声明了这一点并导入了我的自定义单元格...

VPCustomCell *cell;

我难以理解的是错误是什么问题?

【问题讨论】:

  • 您在任何地方都没有cell 的声明。您使用的是 xibs 还是故事板?
  • tableview 的故事板和自定义单元格的 xib
  • 您可以在情节提要中实现自定义单元格,这将是一种更简单的方法,并且还允许您使用dequeueReusableCellWithIdentifier:。您需要做的就是将您的 tableview 设置为使用原型内容,然后在那里显示的单元格您可以更改为您的自定义单元格类,并在那里插入 webview
  • 这也行,但为什么我尝试的方式不行呢?
  • 你在哪里分配 TableViewCell?

标签: iphone objective-c ios cocoa-touch uitableview


【解决方案1】:

在 Interface Builder 中,查看 Identity Inspector (option-command-3) 并确保已将表格 vie 单元格的类设置为 CustomCell 单元格类。

【讨论】:

    【解决方案2】:

    使用 UITableViewCells 时,最好处理标签而不是 outlet。对于您的问题,我会删除网点,在属性检查器中给 Webview 一个标签或任何您想要的,比如 100。对标签执行相同的操作。然后在你的 cellForRowAtIndexPath 中:

    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    NSString *htmlString = @"<html><head> <meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 120\"/></head> <body style=\"background:#FFF;margin-top:0px;margin-left:0px\"> <div><object width=\"120\" height=\"80\"> <param name=\"movie\" value=\"http://www.youtube.com/v/1Xqn5IHbusA&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param> <param name=\"wmode\" value=\"transparent\"></param> <embed src=\"http://www.youtube.com/v/1Xqn5IHbusA&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"120\" height=\"80\"></embed> </object></div></body></html>";
    // Configure the cell...
    UIWebView *thisWebView = (UIWebView*)[cell viewWithTag:100];
    UILabel *thisLabel = (UILabel*)[cell viewWithTag:whateverYouWant];
    [thisWebView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];
    thisLabel.text = @"Hello";
    return cell;
    

    你应该很好

    【讨论】:

    • 问题更可能是dequeueReusableCellWithIdentifier 正在返回默认的UITableViewCell 而不是自定义子类
    • 就我个人而言,我永远不会这样做,因为在代码中使用标签会使代码变得非常混乱且难以阅读。但是,如果您确实想这样做,至少对标签使用常量,因为它会使代码更清晰。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多